---
title: "Diabetes Hypertension care and multiple morbidities"
author: "Jonas Prenissl"
date: "6/9/2018"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
# Data cleaning and merging
Load libraries.
```{r load packages}
library(tidyverse) 
library(dplyr) 
library(forcats) # for categorical variables (R for data science rec) --> see https://rdrr.io/cran/forcats/man/fct_unify.html
library(stringr) # for manipulating string variables (R for data science rec)
#library(lubridate) # for dates and times (R for data science rec)
#instalibrary(dummies) # to easily create dummies
library(ggplot2) 
library(ggrepel) # to avoid text labels in ggplot from overlapping
library(modelr) # to use "add_predictions()" for adding a column of predicted vals to your dataset
library(broom) # to create tidy data from model output
#library(margins) # R equivalent of Stata's margins command --> Thomas Leeper said this only to be used for marginal effects (not prediction)
#library(prediction) # Thomas Leeper's R package to get predicted probabilities
library(srvyr)  # survey package that also works with dplyr 
library(lmtest) # for likelihood ratio tests
library(sandwich) # for robust standard errors 
#library(multiwayvcov) # for clustered standard errors
library(miceadds) # package to cluster SEs more easily than in multiwayvcov; it uses multiwayvcov, so the results between the two packages are exactly the same. 
library(speedglm)
library(foreign) # for importing non-csv datasets
library(readstata13) # foreign only reads Stata 12 or lower
#library(lme4) # for multi-level modeling
#library(lmerTest) # for p-values with the lmer command
#library(sjPlot) # for plotting lmer models
#library(texreg) # for tables
library(tableone) # Creates a table 1 (summary characteristics)
#library(mice) # md.pattern() function to see patterns of missing data 
#library(reshape) # to use the rescalar function

#library(car) # for easy attaching of new variables
#library(arm)
#library(mosaic)
#library(mosaicData)
library(mediation)  # for mediation analysis
#library(lattice)
#library(pander)
library(foreign)
library(clusterSEs)
library(DataCombine)




#DHS.India.updated <- read.csv("~/Documents/Public Health Files/Public Health/public health/DHS with smoking and smokeless.csv")
#dhs <- DHS.India.updated
#dhs <- as_tibble(dhs)

DHS.India.04.10 <- read.csv("~/iCloud Drive (Archive)/Documents/Public Health Files/Public Health/Multiple Morbidities/Datasets/DHS with advice smoking.csv")
dhs <- DHS.India.04.10
dhs <- as_tibble(dhs)

setwd("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities")

dhs <- mutate(dhs,
                hypt = ifelse(hypt==1 & bp_ms==0, NA, hypt))
dhs <- mutate(dhs,
              hypt_med = ifelse(hypt_med==1 & bp_ms==0, NA, hypt_med))




dhs <-mutate(dhs,
             ex_htn_broad_ind = ifelse(is.na(ex_htn_narrow_ind)==T | is.na(hypt) ==T | is.na(hypt_med)==T, NA,ifelse(hypt_med==1 | hypt==1 | ex_htn_narrow_ind==1, 1, 0)))
 

dhs <- mutate(dhs, p_wt_new = ifelse(sex==0 & age==15, p_wt* { ( (25101) / (12159708) )   /   ( (3761) / (13739746) ) },
                                     ifelse(sex==0 & age==16,  p_wt* { ( (24702) / (11564358) )   /   ( (3844) / (13027935) ) },
                                            ifelse(sex==0 & age==17,  p_wt* { ( (22674) / (9868018) )   /   ( (3725) / (11349449) ) },
                                                   ifelse(sex==0 & age==18,   p_wt* { ( (25320) / (12937296) )   /   ( (4177) / (15020851) ) },
                                                          ifelse(sex==0 & age==19,   p_wt* { ( (19462) / (10014673) )   /   ( (3203) / (10844415) ) },
                                                                 ifelse(sex==0 & age==20,   p_wt* { ( (23573) / (13990570) )   /   ( (3632) / (14892165) ) },
                                                                        ifelse(sex==0 & age==21,  p_wt* { ( (18442) / (9446694) )   /   ( (3028) / (10532278) ) },
                                                                               ifelse(sex==0 & age==22,   p_wt* { ( (22212) / (11135249) )   /   ( (3472) / (12392976) ) },
                                                                                      ifelse(sex==0 & age==23,   p_wt* { ( (19660) / (9479866) )   /   ( (2989) / (9674189) ) },
                                                                                             ifelse(sex==0 & age==24,   p_wt* { ( (19262) / (9787150) )   /   ( (3061) / (10093085) ) },
                                                                                                    ifelse(sex==0 & age==25,  p_wt* { ( (25318) / (13456554) )   /   ( (3821) / (14311524) ) },
                                                                                                           ifelse(sex==0 & age==26,   p_wt* { ( (19390) / (9761967) )   /   ( (3131) / (10315030) ) },
                                                                                                                  ifelse(sex==0 & age==27,   p_wt* { ( (18113) / (8157318) )   /   ( (2957) / (8552032) ) },
                                                                                                                         ifelse(sex==0 & age==28,   p_wt* { ( (22299) / (11407090) )   /   ( (3413) / (10719926) ) },
                                                                                                                                ifelse(sex==0 & age==29,   p_wt* { ( (15413) / (7286828) )   /   ( (2476) / (7445696) ) },
                                                                                                                                       ifelse(sex==0 & age==30,  p_wt* { ( (27923) / (14770033) )   /   ( (4356) / (15628996) ) },
                                                                                                                                              ifelse(sex==0 & age==31,   p_wt* { ( 13757 / (6665743) )   /   ( (2190) / (7157502) ) },
                                                                                                                                                     ifelse(sex==0 & age==32, p_wt* { ( (20375) / (8812439) )   /   ( (3156) / (8801105) ) },
                                                                                                                                                            ifelse(sex==0 & age==33,  p_wt* { ( (14514) / (6655662) )   /   ( (2299) / (6108879) ) },
                                                                                                                                                                   ifelse(sex==0 & age==34,   p_wt* { ( (14285) / (7030400) )   /   ( (2348) / (6964192) ) },
                                                                                                                                                                          ifelse(sex==0 & age==35,   p_wt* { ( (27035) / (13385965) )   /   ( (4391) / (15036666) ) },
                                                                                                                                                                                 ifelse(sex==0 & age==36,   p_wt* { ( (15670) / (7760149) )   /   ( (2435) / (8067568) ) },
                                                                                                                                                                                        ifelse(sex==0 & age==37,   p_wt* { ( (13795) / (5907352) )   /   ( (2176) / (5784879) ) },
                                                                                                                                                                                               ifelse(sex==0 & age==38,   p_wt* { ( (18693) / (9381357) )   /   ( (2723) / (8090401) ) },
                                                                                                                                                                                                      ifelse(sex==0 & age==39,   p_wt* { ( (12683) / (5786480) )   /   ( (1968) / (5939867) ) },
                                                                                                                                                                                                             ifelse(sex==0 & age==40,   p_wt* { ( (25682) / (13355581) )   /   ( (3974) / (15173411) ) },
                                                                                                                                                                                                                    ifelse(sex==0 & age==41,  p_wt* { ( (11046) / (5395597) )   /   ( (1819) / (6172297) ) },
                                                                                                                                                                                                                           ifelse(sex==0 & age==42,  p_wt* { ( (16124) / (6523816) )   /   ( (2544) / (6856826) ) },
                                                                                                                                                                                                                                  ifelse(sex==0 & age==43,   p_wt* { ( (11983) / (4865438) )   /   ( (1797) / (4468914) ) },
                                                                                                                                                                                                                                         ifelse(sex==0 & age==44,   p_wt* { ( (10836) / (4752294) )   /   ( (1714) / (4873938) ) },
                                                                                                                                                                                                                                                ifelse(sex==0 & age==45,  p_wt* { ( (23877) / (11187786) )   /   ( (3854) / (12685175) ) },
                                                                                                                                                                                                                                                       ifelse(sex==0 & age==46,  p_wt* { ( (11752) / (5257138) )   /   ( (1895) / (5735540) ) },
                                                                                                                                                                                                                                                              ifelse(sex==0 & age==47,   p_wt* { ( (11295) / (3908175) )   /   ( (1714) / (4043122) ) },
                                                                                                                                                                                                                                                                     ifelse(sex==0 & age==48, p_wt* { ( (14786) / (6081038) )   /   ( (2119) / (5568554) ) },
                                                                                                                                                                                                                                                                            ifelse(sex==0 & age==49,   p_wt* { ( (10399) / (3746076) )   /   ( (1506) / (4105723)) }, p_wt ))))))))))))))))))))))))))))))))))))
dhs <- mutate(dhs, p_wt_new = ifelse(sex==0 & age>=50, p_wt_new/1.013194, p_wt_new )) 
dhs <- mutate(dhs, p_wt_new = ifelse(sex==0 & age>=50, p_wt * { ((6.72342) * (1.013194)) / (0.998047) }, p_wt_new ))   


dhs <- mutate(dhs,
                     bmi_group = ifelse(bmi<18.5, "<18.5kg/m2",
                                        ifelse(bmi>=18.5 & bmi<23, "18.5-22.9 kg/m2",
                                               ifelse(bmi>=23 & bmi<25, "23.0-24.9 kg/m2",
                                                      ifelse(bmi>=25 & bmi<27.5, "25.0-27.4 kg/m2",
                                                             ifelse(bmi>=27.5 & bmi<30, "27.5-29.9 kg/m2",
                                                                    ifelse(bmi>=30, ">= 30.0 kg/m2", NA)))))))


dhs$bmi_group <- factor(dhs$bmi_group, levels = c("<18.5kg/m2", "18.5-22.9 kg/m2", "23.0-24.9 kg/m2", "25.0-27.4 kg/m2", "27.5-29.9 kg/m2", ">= 30.0 kg/m2"))
dhs <- within(dhs, bmi_group<- relevel(bmi_group, ref = "18.5-22.9 kg/m2"))



#### anemia definition

dhs <- dplyr::mutate(dhs,
                           mild_anemia = ifelse((sex = 1 & ex_hb_adj_ind < 12 & ex_hb_adj_ind >= 11) | (sex = 0 & ex_hb_adj_ind < 13 &ex_hb_adj_ind >= 11 ), 1, 0))
dhs$mild_anemia <- factor(dhs$mild_anemia, levels = c("0", "1"))

dhs <- dplyr::mutate(dhs,
                            moderate_anemia = ifelse((sex = 1 & ex_hb_adj_ind < 11 & ex_hb_adj_ind >= 8) | (sex = 0 & ex_hb_adj_ind < 11 & ex_hb_adj_ind >= 8 ), 1, 0))
dhs$moderate_anemia <- factor(dhs$moderate_anemia, levels = c("0", "1"))

dhs <- dplyr::mutate(dhs,
                            severe_anemia = ifelse((sex = 1 & ex_hb_adj_ind < 8) | (sex = 0 & ex_hb_adj_ind < 8 ), 1, 0))
dhs$severe_anemia <- factor(dhs$severe_anemia, levels = c("0", "1")) 


dhs <- dplyr::mutate(dhs,
                           anemia = ifelse(moderate_anemia==1 | severe_anemia==1,1,0))
dhs$anemia <- factor(dhs$anemia, levels = c("0", "1")) 



# 3. Filter out those pregnant  ##########################################################################################################
dhs <- dplyr::filter(dhs, pregnant == 0)# only keep those who aren't pregnant (they didn't measure glucose in pregnant women anyway); dhs_nomiss has no missings in the pregnant variable



##Define diabetics

dhs <- mutate(dhs, ex_glucose_ind=
                            ifelse(dhs$ex_glucose_ind>900 | is.na(ex_glucose_ind)==T,NA,ex_glucose_ind))
dhs <- mutate(dhs, fbg=
                ifelse(dhs$fbg>49.95 | is.na(fbg)==T,NA,fbg))
dhs <- mutate(dhs, ex_glucose_ind = ex_glucose_ind*1.11)


dhs <- mutate(dhs,
              ex_diab_narrow_ind = ifelse( is.na(ex_glucose_ind)==T, NA, 
                                           ifelse(ex_glucose_ind>=200, 1, 
                                                  ifelse(fast==1 & ex_glucose_ind>=126, 1, 0))))


dhs$ex_diab_narrow_ind <- as.factor(dhs$ex_diab_narrow_ind)

dhs <-mutate(dhs,
             ex_diab_broad_ind =  ifelse(is.na(ex_diab_narrow_ind) ==T | is.na(hbg12)==T, NA,  # wir definieren unser sample als diejenige, fuer die wir die Variablen haben, um bestimmen zu koennen ob sie diab broad haben
                                         ifelse(hbg12==1 | ex_diab_narrow_ind==1, 1, 0)))
dhs$ex_diab_broad_ind <- as.factor(dhs$ex_diab_broad_ind)



####define obese

dhs <- mutate(dhs, 
               obese = ifelse(bmi>=27.5,1,0))



#####Filter out those for which we can not make the cascades

dhs_nomiss <- filter(dhs, is.na(dhs$ex_glucose_ind)==F &  is.na(dhs$fast)==F  & is.na(dhs$hbg12)==F & is.na(dhs$ex_htn_narrow_ind) == F & is.na(dhs$hypt) == F & is.na(dhs$hypt_med) == F & is.na(dhs$bp_ms) == F)



#works missing weights as average

dhs_nomiss <- mutate(dhs_nomiss, 
              p_wt_new = ifelse(is.na(p_wt_new)==TRUE, mean(p_wt_new, na.rm=TRUE), p_wt_new))

dhs_nomiss <- dplyr::mutate(dhs_nomiss, 
                     p_wt_newfemale = ifelse(sex==1, dhs_nomiss$p_wt_new, NA))

dhs_nomiss <- dplyr::mutate(dhs_nomiss, 
                     p_wt_newmale = ifelse(sex==0, dhs_nomiss$p_wt_new, NA))







# 9. Create age group women and men, educat group in dhs ##############################################################################
dhs <- dplyr::mutate(dhs, age_grp = ifelse(age<=24, "15-24", 
                                                 ifelse(age>24 & age<=34, "25-34",
                                                        ifelse(age>34 & age<=44, "35-44",
                                                               ifelse(age>44 & age<=49, "45-49","50-54"))))) 
dhs$age_grp <- factor(dhs$age_grp, levels = c("15-24", "25-34", "35-44", "45-49", "50-54"))
dhs <- within(dhs, age_grp <- relevel(age_grp, ref = "15-24"))


#age group by sex


dhs <- dplyr::mutate(dhs, age_grp_women = ifelse(age<=24 & sex==1, "15-24", 
                                                               ifelse(age>24 & sex==1 & age<=34, "25-34",
                                                                      ifelse(age>34 & sex==1 & age<=44, "35-44",
                                                                             ifelse(age>44 & sex==1 & age<=49, "45-49",">49"))))) 
dhs$age_grp_women <- factor(dhs$age_grp_women, levels = c("15-24", "25-34", "35-44", "45-49", ">49"))
dhs <- within(dhs, age_grp_women <- relevel(age_grp_women, ref = "15-24"))


dhs <- dplyr::mutate(dhs, age_grp_men = ifelse(age>14 & sex==0 & age<=24, "15-24", 
                                                             ifelse(age>24 & sex==0 & age<=34, "25-34",
                                                                    ifelse(age>34 & sex==0 & age<=44, "35-44",
                                                                           ifelse(age>44 & sex==0 & age<=54, "45-54", ">54")))))
dhs$age_grp_men <- factor(dhs$age_grp_men, levels = c("15-24", "25-34", "35-44", "45-54", ">54"))
dhs <- within(dhs, age_grp_men <- relevel(age_grp_men, ref = "15-24"))

##educatgroups

dhs <- dplyr::mutate(dhs, educat = ifelse(educat==1 | educat==0 , 0, educat))

dhs <- dplyr::mutate(dhs, educatnames = 
                                           ifelse(educat==1 | educat==0, "Primary schoon unfinished",
                                                                    ifelse(educat==2, "Primary school finished",
                                                                           ifelse(educat==3, "Secondary school unfinished",
                                                                                  ifelse(educat==4, "Secondary school or above", "Refused"))))) 
dhs$educatnames <- factor(dhs$educatnames, levels = c( "Primary schoon unfinished", "Primary school finished", "Secondary school unfinished", "Secondary school or above", "Refused"))
dhs <- within(dhs, educatnames<- relevel(educatnames, ref = "Primary schoon unfinished"))

##marriedgroups
dhs <- dplyr::mutate(dhs, maritalnames = ifelse(marital==1, "Never married", 
                                                              ifelse(marital==2, "Currently married",
                                                                     ifelse(marital==3, "Separated",
                                                                            ifelse(marital==4, "Divorced",
                                                                                   ifelse(marital==5, "Widowed",
                                                                                          ifelse(marital==6, "Cohabitating",
                                                                                                 ifelse(marital==7, "divorced or separated or deserted or widowed","Refused")))))))) 
dhs$maritalnames <- factor(dhs$maritalnames, levels = c("Never married", "Currently married", "Separated", "Divorced", "Widowed", "Cohabitating", "divorced/separated/deserted/widowed", "Refused"))
dhs <- within(dhs, maritalnames<- relevel(maritalnames, ref = "Never married"))

###create married variable in dhs


dhs <- mutate( dhs,
                      married = ifelse( dhs$marital==2, 1, 0))

dhs$married <- as.factor(dhs$married)


dhs <- mutate( dhs,
                      marriednames = ifelse( dhs$marital==2, "Married","Not married"))

dhs$marriednames <- as.factor(dhs$marriednames)

dhs <- within(dhs, marriednames<- relevel(marriednames, ref = "Not married"))



dhs <- mutate(dhs,
                     age_grpOR = 
                       as.factor(ifelse(
                         age>=15 & age<20, "15-19",
                         ifelse(age>=20 & age<25, "20-24",
                                ifelse(age>=25 & age<30, "25-29",
                                       ifelse(age>=30 & age<35,"30-34", 
                                              ifelse(age>=35 & age<40, "35-39",
                                                     ifelse(age>=40 & age<45, "40-44",
                                                            ifelse(age>=45 & age<50, "45-49",
                                                                   ifelse(age>=50 & age<55, "50-54",NA))))))))))





# Diabetes medication as factor in dhs

dhs$ex_dia_med_ind <- as.factor(dhs$ex_dia_med_ind)

######wealth_quintile_rurb as factor in dhs

dhs$wealth_quintile_rurb <- as.factor(dhs$wealth_quintile_rurb)


# create labels for female, rural, rich in dhs

dhs <- mutate(dhs, 
                     rural = ifelse(urban==1, 0, 1),
                     female = ifelse(sex==1, 1, 0),
                     male = ifelse(sex==1, 0, 1))
dhs$rural <- as.factor(dhs$rural)
dhs$female <- as.factor(dhs$female)
dhs$male <- as.factor(dhs$male)



dhs <- dhs %>% 
  mutate(female_lab = as.factor(ifelse(is.na(female) == TRUE, NA, 
                                       ifelse(female == 1, "Female", "Male"))),
         urban_lab = as.factor(ifelse(is.na(rural) == TRUE, NA, 
                                      ifelse(rural == 1, "Rural", "Urban"))),
         wealth_quintile_rurb_lab = as.factor(ifelse(is.na(wealth_quintile_rurb) == TRUE, NA, 
                                                     ifelse( wealth_quintile_rurb == 1, "Q1 (Poorest)", 
                                                             ifelse(wealth_quintile_rurb == 2, "Q2",
                                                                    ifelse(wealth_quintile_rurb == 3, "Q3",
                                                                           ifelse(wealth_quintile_rurb == 4, "Q4",
                                                                                  ifelse(wealth_quintile_rurb == 5, "Q5 (Richest)", NA))))))))

dhs$wealth_quintile_rurb_lab <- as.factor(dhs$wealth_quintile_rurb_lab)
dhs<- within(dhs, wealth_quintile_rurb_lab <- relevel(wealth_quintile_rurb_lab, ref = "Q1 (Poorest)"))




# 9. Create age group women and men, educat group in dhs_nomiss ##############################################################################

###create married variable

dhs_nomiss <- mutate( dhs_nomiss,
                      marriednames = ifelse( dhs_nomiss$marital==2, "Married","Not married"))
###Age groups

dhs_nomiss <- dplyr::mutate(dhs_nomiss, age_grp = ifelse(age<=24, "15-24", 
                                           ifelse(age>24 & age<=34, "25-34",
                                                  ifelse(age>34 & age<=44, "35-44","45-54")))) 
dhs_nomiss$age_grp <- factor(dhs_nomiss$age_grp, levels = c("15-24", "25-34", "35-44", "45-54"))
dhs_nomiss <- within(dhs_nomiss, age_grp <- relevel(age_grp, ref = "15-24"))



##age group per sex

dhs_nomiss <- dplyr::mutate(dhs_nomiss, age_grp_women = ifelse(age<=24 & sex==1, "15-24", 
                                                 ifelse(age>24 & sex==1 & age<=34, "25-34",
                                                        ifelse(age>34 & sex==1 & age<=44, "35-44", 
                                                               ifelse(age>44 &sex==1 & age<=49, "45-49", ">49"))))) 
dhs_nomiss$age_grp_women <- factor(dhs_nomiss$age_grp_women, levels = c("15-24", "25-34", "35-44", "45-49", ">49"))

dhs_nomiss <- within(dhs_nomiss, age_grp_women <- relevel(age_grp_women, ref = "15-24"))


dhs_nomiss <- dplyr::mutate(dhs_nomiss, age_grp_men = ifelse(age>14 & sex==0 & age<=24, "15-24", 
                                               ifelse(age>24 & sex==0 & age<=34, "25-34",
                                                      ifelse(age>34 & sex==0 & age<=44, "35-44",
                                                             ifelse(age>44 & sex==0 & age<=54, "45-54", ">54"))))) 
dhs_nomiss$age_grp_men <- factor(dhs_nomiss$age_grp_men, levels = c("15-24", "25-34", "35-44", "45-54", ">54"))
dhs_nomiss <- within(dhs_nomiss, age_grp_men <- relevel(age_grp_men, ref = "15-24"))

###age group for educat analysis

dhs_nomiss <- dplyr::mutate(dhs_nomiss, age_grp_educat = ifelse(age>18 & age<=26, "19-26", 
                                                         ifelse(age>26 & age<=34, "27-34",
                                                                ifelse(age>34 & age<=42, "35-42",
                                                                       ifelse(age>42 & age<=49, "43-49", NA))))) 
dhs_nomiss$age_grp_educat <- factor(dhs_nomiss$age_grp_educat, levels = c("19-26", "27-34", "35-42", "43-49"))
dhs_nomiss <- within(dhs_nomiss, age_grp_educat <- relevel(age_grp_educat, ref = "19-26"))

dhs_nomiss <- dplyr::mutate(dhs_nomiss, age_grp_educat_men =  ifelse(age>18 & age<=26, "19-26", 
                                                                     ifelse(age>26 & age<=34, "27-34",
                                                                            ifelse(age>34 & age<=42, "35-42",
                                                                                   ifelse(age>42 , "43-54", NA))))) 
dhs_nomiss$age_grp_educat_men <- factor(dhs_nomiss$age_grp_educat_men, levels = c("19-26", "27-34", "35-42", "43-54"))
dhs_nomiss <- within(dhs_nomiss, age_grp_educat_men <- relevel(age_grp_educat_men, ref = "19-26"))



####educat reform


dhs_nomiss <- dplyr::mutate(dhs_nomiss, educat = ifelse(educat==0 | educat==1 , 0, educat))

dhs_nomiss <- dplyr::mutate(dhs_nomiss, educatnames = 
                       ifelse( educat==0, "Primary school unfinished",
                              ifelse(educat==2, "Primary school finished",
                                     ifelse(educat==3, "Secondary school unfinished",
                                            ifelse(educat==4, "Secondary school or above", "Refused"))))) 
dhs_nomiss$educatnames <- factor(dhs_nomiss$educatnames, levels = c( "Primary school unfinished", "Primary school finished", "Secondary school unfinished", "Secondary school or above", "Refused"))
dhs_nomiss <- within(dhs_nomiss, educatnames<- relevel(educatnames, ref = "Primary school unfinished"))



###married subgroups

dhs_nomiss <- dplyr::mutate(dhs_nomiss, maritalnames = ifelse(marital==1, "Never married", 
                                                ifelse(marital==2, "Currently married",
                                                       ifelse(marital==3, "Separated",
                                                              ifelse(marital==4, "Divorced",
                                                                     ifelse(marital==5, "Widowed",
                                                                            ifelse(marital==6, "Cohabitating",
                                                                                   ifelse(marital==7, "divorced or separated or deserted or widowed","Refused")))))))) 


dhs_nomiss$maritalnames <- factor(dhs_nomiss$maritalnames, levels = c("Never married", "Currently married", "Separated", "Divorced", "Widowed", "Cohabitating", "divorced/separated/deserted/widowed", "Refused"))
dhs_nomiss <- within(dhs_nomiss, maritalnames<- relevel(maritalnames, ref = "Never married"))




###create married variable

dhs_nomiss <- mutate( dhs_nomiss,
                      married = ifelse( dhs_nomiss$marital==2, 1, 0))

dhs_nomiss$married <- as.factor(dhs_nomiss$married)


dhs_nomiss <- mutate( dhs_nomiss,
                      marriednames = ifelse( dhs_nomiss$married==1, "Married","Not married"))

dhs_nomiss$marriednames <- as.factor(dhs_nomiss$marriednames)

dhs_nomiss <- within(dhs_nomiss, marriednames<- relevel(marriednames, ref = "Not married"))



dhs_nomiss <- mutate(dhs_nomiss,
                                   age_grpOR = 
                                     as.factor(ifelse(
                                       age>=15 & age<20, "15-19",
                                       ifelse(age>=20 & age<25, "20-24",
                                              ifelse(age>=25 & age<30, "25-29",
                                                     ifelse(age>=30 & age<35,"30-34", 
                                                            ifelse(age>=35 & age<40, "35-39",
                                                                   ifelse(age>=40 & age<45, "40-44",
                                                                          ifelse(age>=45 & age<50, "45-49",
                                                                                 ifelse(age>=50 & age<55, "50-54",NA))))))))))

dhs_nomiss$age_grpOR <- factor(dhs_nomiss$age_grpOR, levels = c("15-19","20-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54"))

dhs_nomiss <- within(dhs_nomiss, age_grpOR <- relevel(age_grpOR, ref = "15-19"))



### test if unique and number
#unique(dhs$d_id)
#length(unique(dhs$d_id))
#length(unique(dhs$psu))


dhs_nomiss$tobacco_received_advice_stop<- as.numeric(dhs_nomiss$tobacco_received_advice_stop)


# Diabetes medication as factor

dhs_nomiss$ex_dia_med_ind <- as.factor(dhs_nomiss$ex_dia_med_ind)

######wealth_quintile_rurb as factor

dhs_nomiss$wealth_quintile_rurb <- as.factor(dhs_nomiss$wealth_quintile_rurb)


# create labels for female, rural, rich

dhs_nomiss <- mutate(dhs_nomiss, 
              rural = ifelse(urban==1, 0, 1),
              female = ifelse(sex==1, 1, 0),
              male = ifelse(sex==1, 0, 1))
dhs_nomiss$rural <- as.factor(dhs_nomiss$rural)
dhs_nomiss$female <- as.factor(dhs_nomiss$female)
dhs_nomiss$male <- as.factor(dhs_nomiss$male)



dhs_nomiss <- dhs_nomiss %>% 
  mutate(female_lab = as.factor(ifelse(is.na(female) == TRUE, NA, 
                                       ifelse(female == 1, "Female", "Male"))),
         urban_lab = as.factor(ifelse(is.na(rural) == TRUE, NA, 
                                      ifelse(rural == 1, "Rural", "Urban"))),
         wealth_quintile_rurb_lab = as.factor(ifelse(is.na(wealth_quintile_rurb) == TRUE, NA, 
                                                     ifelse( wealth_quintile_rurb == 1, "Q1 (Poorest)", 
                                                             ifelse(wealth_quintile_rurb == 2, "Q2",
                                                             ifelse(wealth_quintile_rurb == 3, "Q3",
                                                                    ifelse(wealth_quintile_rurb == 4, "Q4",
                                                             ifelse(wealth_quintile_rurb == 5, "Q5 (Richest)", NA))))))))

dhs_nomiss$wealth_quintile_rurb_lab <- as.factor(dhs_nomiss$wealth_quintile_rurb_lab)
dhs_nomiss<- within(dhs_nomiss, wealth_quintile_rurb_lab <- relevel(wealth_quintile_rurb_lab, ref = "Q1 (Poorest)"))


######COMBINED CASCADE

##aware diabetic as subset of diabetics



dhs_nomiss <- mutate(dhs_nomiss,
                     both_aware = ifelse( hbg12==1 & hypt==1, 1, 0))

dhs_nomiss$both_aware <- as.factor(dhs_nomiss$both_aware)

dhs_nomiss <- mutate(dhs_nomiss,
                     
                     both_aware_dbl = as.numeric(both_aware)-1)

summary(dhs_nomiss$both_aware)

##treated both as subset boths


dhs_nomiss <- mutate(dhs_nomiss,
                                   both_treated = ifelse(ex_dia_med_ind==1 & hypt_med==1, 1, 0))
dhs_nomiss[which(is.na(dhs_nomiss$both_treated)==T), "both_treated"]<-0

dhs_nomiss$both_treated <- as.factor(dhs_nomiss$both_treated)

dhs_nomiss <- mutate(dhs_nomiss,
                                   
                                   both_treated_dbl = as.numeric(both_treated)-1)

summary(dhs_nomiss$both_treated)

##controlled both as subset of  boths

dhs_nomiss <- mutate(dhs_nomiss,
              newcutoff = ifelse( is.na(ex_glucose_ind)==T, NA, 
                                           ifelse(ex_glucose_ind<=182, 1, 0)))


dhs_nomiss <- mutate(dhs_nomiss,
                                             diab_controlled = ifelse((ex_dia_med_ind==1 & newcutoff==1), 1, 0))

dhs_nomiss <- mutate(dhs_nomiss,
                                             htn_controlled = ifelse((hypt_med==1 & ex_htn_narrow_ind==0), 1, 0))


dhs_nomiss <- mutate(dhs_nomiss,
                                             both_controlled = ifelse((diab_controlled==1 & htn_controlled==1), 1, 0))




dhs_nomiss$both_controlled <- as.factor(dhs_nomiss$both_controlled)

dhs_nomiss <- mutate(dhs_nomiss,
                                             
                                             both_controlled_dbl = as.numeric(both_controlled)-1)

summary(dhs_nomiss$both_controlled)








#######DIABETES CASCADE

##aware diabetic as subset of diabetics



dhs_nomiss <- mutate(dhs_nomiss,
                     diabetic_aware = ifelse( hbg12==1, 1, 0))

dhs_nomiss$diabetic_aware <- as.factor(dhs_nomiss$diabetic_aware)

dhs_nomiss <- mutate(dhs_nomiss,
                     
                     diabetic_aware_dbl = as.numeric(diabetic_aware)-1)

summary(dhs_nomiss$diabetic_aware)

##treated diabetic as subset diabetics


dhs_nomiss <- mutate(dhs_nomiss,
                                   diabetic_treated = ifelse(ex_dia_med_ind==1, 1, 0))
dhs_nomiss[which(is.na(dhs_nomiss$diabetic_treated)==T), "diabetic_treated"]<-0

dhs_nomiss$diabetic_treated <- as.factor(dhs_nomiss$diabetic_treated)

dhs_nomiss <- mutate(dhs_nomiss,
                                   
                                   diabetic_treated_dbl = as.numeric(diabetic_treated)-1)

summary(dhs_nomiss$diabetic_treated)

##controlled diabetic as subset of  diabetics

dhs_nomiss <- mutate(dhs_nomiss,
              newcutoff = ifelse( is.na(ex_glucose_ind)==T, NA, 
                                           ifelse(ex_glucose_ind<=182, 1, 0)))


dhs_nomiss <- mutate(dhs_nomiss,
                                             diabetic_controlled = ifelse((ex_dia_med_ind==1 & newcutoff==1), 1, 0))

dhs_nomiss$diabetic_controlled <- as.factor(dhs_nomiss$diabetic_controlled)

dhs_nomiss <- mutate(dhs_nomiss,
                                             
                                             diabetic_controlled_dbl = as.numeric(diabetic_controlled)-1)

summary(dhs_nomiss$diabetic_controlled)


############## HTN CASCADE



##screened htn

dhs_nomiss <- mutate(dhs_nomiss,
                     htn_screened = bp_ms)

dhs_nomiss$htn_screened <- as.factor(dhs_nomiss$htn_screened)

dhs_nomiss <- mutate(dhs_nomiss,
                              
                              htn_screened_dbl = as.numeric(htn_screened)-1)

summary(dhs_nomiss$htn_screened)


##aware htn



## Test if there are paradoxically treated but unaware patients
dhs_nomiss <- mutate(dhs_nomiss,
                     treated_and_unaware = ifelse( hypt==0 &hypt_med==1, 1, 0))
dhs_nomiss$treated_and_unaware <- as.factor(dhs_nomiss$treated_and_unaware)
summary(dhs_nomiss$treated_and_unaware)

dhs_nomiss <- mutate(dhs_nomiss,
                             treated_and_unaware = ifelse( htn_aware==0 &htn_treated==1, 1, 0))
dhs_nomiss$treated_and_unaware <- as.factor(dhs_nomiss$treated_and_unaware)
summary(dhs_nomiss$treated_and_unaware)

##aware htn as subset of htn



dhs_nomiss <- mutate(dhs_nomiss,
                     htn_aware = ifelse(hypt==1 | hypt_med==1, 1, 0))


dhs_nomiss$htn_aware <- as.factor(dhs_nomiss$htn_aware)



dhs_nomiss <- mutate(dhs_nomiss,
                     
                     htn_aware_dbl = as.numeric(htn_aware)-1)

summary(dhs_nomiss$htn_aware)

##treated htn as subset of htns


dhs_nomiss <- mutate(dhs_nomiss,
                                   htn_treated = ifelse(hypt_med==1, 1, 0))
#dhs_nomiss[which(is.na(dhs_nomiss$htn_treated)==T), "htn_treated"]<-0

dhs_nomiss$htn_treated <- as.factor(dhs_nomiss$htn_treated)

dhs_nomiss <- mutate(dhs_nomiss,
                                   
                                   htn_treated_dbl = as.numeric(htn_treated)-1)

summary(dhs_nomiss$htn_treated)

##controlled htn as subset of  htns

dhs_nomiss <- mutate(dhs_nomiss,
                                             htn_controlled = ifelse((hypt_med==1 & ex_htn_narrow_ind==0), 1, 0))

dhs_nomiss$htn_controlled <- as.factor(dhs_nomiss$htn_controlled)

dhs_nomiss <- mutate(dhs_nomiss,
                                             
                                             htn_controlled_dbl = as.numeric(htn_controlled)-1)

summary(dhs_nomiss$htn_controlled)


###paradoxically aware but unscreened

dhs_nomiss <- mutate(dhs_nomiss,
                              aware_and_unscreened = ifelse( htn_aware==1 & htn_screened==0, 1, 0))
dhs_nomiss$aware_and_unscreened <- as.factor(dhs_nomiss$aware_and_unscreened)
summary(dhs_nomiss$aware_and_unscreened)


###paradoxically treated and unscreened

dhs_nomiss <- mutate(dhs_nomiss,
                              treated_and_unscreened = ifelse( htn_treated==1 & htn_screened==0, 1, 0))
dhs_nomiss$treated_and_unscreened <- as.factor(dhs_nomiss$treated_and_unscreened)
summary(dhs_nomiss$treated_and_unscreened)

## Test if there are paradoxically treated but unaware patients
dhs_nomiss <- mutate(dhs_nomiss,
                     treated_and_unaware = ifelse( htn_aware==0 & htn_treated==1, 1, 0))
dhs_nomiss$treated_and_unaware <- as.factor(dhs_nomiss$treated_and_unaware)
summary(dhs_nomiss$treated_and_unaware)






######make morbidity dummies for both diseases if both aware,treated,controlled

dhs_nomiss <- mutate(dhs_nomiss,
                              both_aware= ifelse( htn_aware==1 & diabetic_aware==1, 1, 0,
                                                  both_treated= ifelse(htn_treated==1 & diabetic_treated==1,1,0,
                                                                       both_controlled = ifelse(htn_controlled==1 & diabetic_controlled==1))))



########basic info morbidities

dhs_nomiss$ex_htn_broad_ind_factor <- as.factor(dhs_nomiss$ex_htn_broad_ind)
dhs_nomiss$ex_diab_broad_ind_factor <- as.factor(dhs_nomiss$ex_diab_broad_ind)
dhs_nomiss$hiv03_factor <- as.factor(dhs_nomiss$hiv03)
dhs_nomiss$heart_factor <- as.factor(dhs_nomiss$heart)
dhs_nomiss$cancer_factor <- as.factor(dhs_nomiss$cancer)
dhs_nomiss$thyroid_factor <- as.factor(dhs_nomiss$thyroid)
dhs_nomiss$anemia_factor <- as.factor(dhs_nomiss$anemia)
dhs_nomiss$asthma_factor <- as.factor(dhs_nomiss$asthma)

summary(dhs_nomiss$ex_htn_broad_ind_factor)
summary(dhs_nomiss$ex_diab_broad_ind_factor)
summary(dhs_nomiss$hiv03_factor)
summary(dhs_nomiss$heart_factor)
summary(dhs_nomiss$cancer_factor)
summary(dhs_nomiss$thyroid_factor)
summary(dhs_nomiss$anemia_factor)
summary(dhs_nomiss$asthma_factor)

dhs_nomiss<- mutate(dhs_nomiss, 
              tobacco_smoked = ifelse(tobacco_smoked==1 | tobacco_smokeless==1,1,0))


#######make numeric morbidities where NA is 0


dhs_nomiss <- mutate(dhs_nomiss, 
              tobacco_smoked_dbl = as.numeric(tobacco_smoked),
              obese_dbl = as.numeric(obese),
              ex_htn_broad_ind_dbl = as.numeric(ex_htn_broad_ind),
              asthma_dbl = as.numeric(asthma),
              anemia_dbl = as.numeric(anemia)-1,
              ex_diab_broad_ind_dbl = as.numeric(ex_diab_broad_ind)-1)
          

summary(dhs_nomiss$tobacco_smoked_dbl)
summary(dhs_nomiss$obese_dbl)
summary(dhs_nomiss$ex_htn_broad_ind_dbl)
summary(dhs_nomiss$asthma_dbl)
summary(dhs_nomiss$anemia_dbl)
summary(dhs_nomiss$anemia_dbl)
summary(dhs_nomiss$ex_diab_broad_ind_dbl)


              


#####make NAs to 0 in dbl

dhs_nomiss[which(is.na(dhs_nomiss$tobacco_smoked_dbl)==T), "tobacco_smoked_dbl"]<-0

dhs_nomiss[which(is.na(dhs_nomiss$obese_dbl)==T), "obese_dbl"]<-0

dhs_nomiss[which(is.na(dhs_nomiss$ex_diab_broad_ind_dbl)==T), "ex_diab_broad_ind_dbl"]<-0

dhs_nomiss[which(is.na(dhs_nomiss$ex_htn_broad_ind_dbl)==T), "ex_htn_broad_ind_dbl"]<-0

dhs_nomiss[which(is.na(dhs_nomiss$asthma_dbl)==T), "asthma_dbl"]<-0

dhs_nomiss[which(is.na(dhs_nomiss$anemia_dbl)==T), "anemia_dbl"]<-0


summary(dhs_nomiss$tobacco_smoked_dbl)
summary(dhs_nomiss$obese_dbl)
summary(dhs_nomiss$ex_diab_broad_ind_dbl)
summary(dhs_nomiss$ex_htn_broad_ind_dbl)
summary(dhs_nomiss$asthma_dbl)
summary(dhs_nomiss$anemia_dbl)



#######treated for all three CVDs


dhs_nomiss<- mutate(dhs_nomiss, 
              three_treated = ifelse(hypt_med==1 & tobacco_received_advice_stop==1 & ex_dia_med_ind==1,1,0))



 #####################make morbidity Dummy

###both################################################################                       

dhs_nomiss<- mutate(dhs_nomiss, 
              sum_smob = obese_dbl   + tobacco_smoked_dbl )

dhs_nomiss <- mutate(dhs_nomiss, 
               sum_smob_dbl = as.numeric(sum_smob))

dhs_nomiss$sum_smob <- as.factor(dhs_nomiss$sum_smob)

summary(dhs_nomiss$sum_smob)
summary(dhs_nomiss$sum_smob_dbl)

####no cvd sum###



dhs_nomiss<- mutate(dhs_nomiss, 
              sum_nocvd = asthma_dbl   + anemia_dbl )

dhs_nomiss <- mutate(dhs_nomiss, 
               sum_nocvd_dbl = as.numeric(sum_nocvd))

dhs_nomiss$sum_nocvd <- as.factor(dhs_nomiss$sum_nocvd)

summary(dhs_nomiss$sum_nocvd)
summary(dhs_nomiss$sum_nocvd_dbl)


 ######make morbidity Dummy Hypertension                       

dhs_nomiss<- mutate(dhs_nomiss, 
              sum_multihtn = ex_diab_broad_ind_dbl  + tobacco_smoked_dbl + obese_dbl)

dhs_nomiss <- mutate(dhs_nomiss, 
               sum_multihtn_dbl = as.numeric(sum_multihtn)-1)
               


dhs_nomiss$sum_multihtn <- as.factor(dhs_nomiss$sum_multihtn)


summary(dhs_nomiss$sum_multihtn)
summary(dhs_nomiss$sum_multihtn_dbl)


 ######make morbidity Dummy diabetes                      

dhs_nomiss<- mutate(dhs_nomiss, 
              sum_multidiab = ex_htn_broad_ind_dbl  + tobacco_smoked_dbl + obese_dbl)

dhs_nomiss <- mutate(dhs_nomiss, 
               sum_multidiab_dbl = as.numeric(sum_multidiab))
               


dhs_nomiss$sum_multidiab <- as.factor(dhs_nomiss$sum_multidiab)


summary(dhs_nomiss$sum_multidiab)
summary(dhs_nomiss$sum_multidiab_dbl)



####morbidity dummy advice quit smoking

dhs_nomiss<- mutate(dhs_nomiss, 
              sum_advice_cvd = ex_htn_broad_ind_dbl  + ex_diab_broad_ind_dbl + obese_dbl)

dhs_nomiss <- mutate(dhs_nomiss, 
               sum_advice_cvd_dbl = as.numeric(sum_advice_cvd))
               


dhs_nomiss$sum_advice_cvd <- as.factor(dhs_nomiss$sum_advice_cvd)


summary(dhs_nomiss$sum_advice_cvd)
summary(dhs_nomiss$sum_advice_cvd_dbl)







#######make numeric TREATED where NA is 0
 

dhs_nomiss <- mutate(dhs_nomiss,
              asthma_treated_dbl = as.numeric(asthma_treated),
              ex_dia_med_ind_dbl = as.numeric(ex_dia_med_ind)-1,
              hypt_med_dbl = as.numeric(hypt_med),
              thyroid_treated_dbl = as.numeric(thyroid_treated),
              heart_treated_dbl = as.numeric(heart_treated),
              cancer_treated_dbl = as.numeric(cancer_treated))

summary(dhs_nomiss$asthma_treated_dbl)
summary(dhs_nomiss$ex_dia_med_ind_dbl)
summary(dhs_nomiss$hypt_med_dbl)
summary(dhs_nomiss$thyroid_treated_dbl)
summary(dhs_nomiss$heart_treated_dbl)
summary(dhs_nomiss$cancer_treated_dbl)


              


#####make NAs to 0 in Treated dbl


dhs_nomiss[which(is.na(dhs_nomiss$asthma_treated_dbl)==T), "asthma_treated_dbl"]<-0
dhs_nomiss[which(is.na(dhs_nomiss$ex_dia_med_ind_dbl)==T), "ex_dia_med_ind_dbl"]<-0
dhs_nomiss[which(is.na(dhs_nomiss$hypt_med_dbl)==T), "hypt_med_dbl"]<-0
dhs_nomiss[which(is.na(dhs_nomiss$thyroid_treated_dbl)==T), "thyroid_treated_dbl"]<-0
dhs_nomiss[which(is.na(dhs_nomiss$heart_treated_dbl)==T), "heart_treated_dbl"]<-0
dhs_nomiss[which(is.na(dhs_nomiss$cancer_treated_dbl)==T), "cancer_treated_dbl"]<-0

summary(dhs_nomiss$asthma_treated_dbl)
summary(dhs_nomiss$ex_dia_med_ind_dbl)
summary(dhs_nomiss$hypt_med_dbl)
summary(dhs_nomiss$thyroid_treated_dbl)
summary(dhs_nomiss$heart_treated_dbl)
summary(dhs_nomiss$cancer_treated_dbl)



 ######make Treated sum diabetes                       

dhs_nomiss<- mutate(dhs_nomiss, 
              sum_diab_treated = asthma_treated_dbl + hypt_med_dbl + thyroid_treated_dbl + heart_treated_dbl + cancer_treated_dbl) 

dhs_nomiss <- mutate(dhs_nomiss, 
               sum_diab_treated_dbl = as.numeric(sum_diab_treated)-1)
               


dhs_nomiss$sum_diab_treated <- as.factor(dhs_nomiss$sum_diab_treated)

summary(dhs_nomiss$sum_diab_treated)

 ######make Treated sum Hypertension  


dhs_nomiss<- mutate(dhs_nomiss, 
              sum_htn_treated = asthma_treated_dbl + ex_dia_med_ind_dbl + thyroid_treated_dbl + heart_treated_dbl + cancer_treated_dbl) 

dhs_nomiss <- mutate(dhs_nomiss, 
               sum_htn_treated_dbl = as.numeric(sum_htn_treated)-1)
               


dhs_nomiss$sum_htn_treated <- as.factor(dhs_nomiss$sum_htn_treated)

summary(dhs_nomiss$sum_htn_treated)









# Summary dhs_nomiss

dhs_nomiss$fast <- as.factor(dhs_nomiss$fast)
dhs_nomiss$ex_htn_broad_ind <- as.factor(dhs_nomiss$ex_htn_broad_ind)
dhs_nomiss$tobacco_smoked <- as.factor(dhs_nomiss$tobacco_smoked)
dhs_nomiss$tobacco_smokeless <- as.factor(dhs_nomiss$tobacco_smokeless)




table1names <- c("ex_diab_broad_ind","ex_htn_broad_ind", "age_grpOR",
                 "age_grp_men", "age_grp_women", "fast", "educatnames", "wealth_quintile_rurb_lab", "bmi_group", "tobacco_smoked", "tobacco_smokeless", "marriednames", "urban_lab")




totalwithmiss <- CreateTableOne(vars = table1names, data=dhs_nomiss, includeNA=T)
total <- print(totalwithmiss)

write.csv(total, file="dhs_nomiss multicascades summary.csv")

sexwithmiss <- CreateTableOne(vars = table1names, data=dhs_nomiss, strata=c("female_lab"), includeNA=T)
sexdhs_nomiss <- print(sexwithmiss)

write.csv(sexdhs_nomiss, file= "dhs_nomiss multicascades summary per sex.csv")






# create stratum ID
dhs_nomiss$state_dist_str <- as.character(dhs_nomiss$ex_state_ind)
dhs_nomiss$rural_str <- as.character(dhs_nomiss$rural)
dhs_nomiss <- mutate(dhs_nomiss, 
              stratumid = str_c(state_dist_str, rural_str, sep = "_")) 
dhs_nomiss$stratumid <- as.factor(dhs_nomiss$stratumid)


##diabetic as numeric

dhs_nomiss <- mutate(dhs_nomiss,
              ex_diab_broad_ind_dbl = as.numeric(dhs_nomiss$ex_diab_broad_ind)-1)


####Check mean, median, 25th, 75th, min and max  in 1) a district, and 2) a PSU, if ex_diab_narrow_ind !=NA 

####check districts



#uniquestates<-unique(dhs_nomiss$ex_state_ind)

#keeptrack4<-NULL

#for(state in (uniquestates)){
 
#  keeptrack4<-c(keeptrack4,sum(dhs_nomiss$ex_state_ind==state))
#}
#names(keeptrack4)<-uniquestates
#summary(keeptrack4)

####check psus


#uniquepsu<-unique(dhs_nomiss_noNAinpsu$psu)

#keeptrack4<-NULL

#for(dis in (uniquepsu)){
  
  #keeptrack4<-c(keeptrack4,sum(dhs_nomiss_noNAinpsu$psu==dis))
#}
#names(keeptrack4)<-uniquepsu
#summary(keeptrack4)



####Create PSUS for subsets besides dhs, always if working with psu, use dataset with NAinpsu filtered for psu==NA




###create PSU ID in dhs_nomiss:
dhs_nomiss <- dhs_nomiss %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss$psuid <- as.factor(dhs_nomiss$psuid) 







dhs_nomiss_diabetic_only <- mutate(dhs_nomiss_diabetic_only,
                              age_grpOR = 
                                as.factor(ifelse(
                                  age>=15 & age<20, "15-19",
                                  ifelse(age>=20 & age<25, "20-24",
                                         ifelse(age>=25 & age<30, "25-29",
                                                ifelse(age>=30 & age<35,"30-34", 
                                                       ifelse(age>=35 & age<40, "35-39",
                                                              ifelse(age>=40 & age<45, "40-44",
                                                                     ifelse(age>=45 & age<50, "45-49",
                                                                            ifelse(age>=50 & age<55, "50-54","54+"))))))))))

dhs_nomiss_diabetic_only$age_grpOR <- factor(dhs_nomiss_diabetic_only$age_grpOR, levels = c("15-19", "20-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54"))

dhs_nomiss_diabetic_only <- within(dhs_nomiss_diabetic_only, age_grpOR <- relevel(age_grpOR, ref = "15-19"))

  dhs_nomiss_rural <- filter(dhs_nomiss, (urban_lab)=="Rural")
  dhs_nomiss_urban <- filter(dhs_nomiss, (urban_lab)=="Urban")
  dhs_nomiss_diabetic_only_rural <- filter(dhs_nomiss_diabetic_only, (urban_lab)=="Rural")
  dhs_nomiss_diabetic_only_urban <- filter(dhs_nomiss_diabetic_only, (urban_lab)=="Urban")
  
  dhs_nomiss_diabetic_only_rural_women <- filter(dhs_nomiss_diabetic_only, (urban_lab)=="Rural" & sex==1)
  dhs_nomiss_diabetic_only_rural_men <- filter(dhs_nomiss_diabetic_only, (urban_lab)=="Rural" & sex==0)
  
  dhs_nomiss_diabetic_only_urban_women <- filter(dhs_nomiss_diabetic_only, (urban_lab)=="Urban" & sex==1)
  dhs_nomiss_diabetic_only_urban_men <- filter(dhs_nomiss_diabetic_only, (urban_lab)=="Urban" & sex==0)
  
  dhs_nomiss_diabetic_only_women <- filter(dhs_nomiss_diabetic_only,  sex==1)
  dhs_nomiss_diabetic_only_men <- filter(dhs_nomiss_diabetic_only,  sex==0)
  
  
dhs <- mutate(dhs, diabneu = ifelse(ex_diab_broad_ind==1 & ex_htn_broad_ind==0 & asthma==0 & ex_anemia_ind==0,1,0))
  
dhs_nomiss <- mutate(dhs_nomiss,
              non_cvd_morb = ifelse(asthma==1,1, 
                                  ifelse(anemia==1,1,0)))


dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1) 

  
  
####NAinpsu for dhs_nomiss

dhs_nomiss_noNAinpsu <- mutate(dhs_nomiss, 
                     psu = ifelse(psu==1, NA, psu))

summary(dhs_nomiss_noNAinpsu$psu)


dhs_nomiss_noNAinpsu <- filter(dhs_nomiss_noNAinpsu, is.na(psu)==F)

summary(dhs_nomiss_noNAinpsu$psu)


```



```{r percentages that have both diabetes and hypertension}


dhs_nomiss <- mutate(dhs_nomiss, 
                     htn_and_diab = ifelse(ex_diab_broad_ind ==1 & ex_htn_broad_ind==1,1,0))


#filter
both_noob_nosmo <- dhs_nomiss

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_and_diab, ex_diab_broad_ind_dbl, ex_htn_broad_ind_dbl))

prevtotall <- svy_treated %>%
  summarize(htn_diab_prop = survey_mean(htn_and_diab, proportion=TRUE, vartype = "ci")*100,
            diab_prop = survey_mean(ex_diab_broad_ind_dbl, proportion=TRUE, vartype = "ci")*100, 
            htn_prop = survey_mean(ex_htn_broad_ind_dbl, proportion=TRUE, vartype = "ci")*100)

write_csv(prevtotall, " percentage both hypertension and diabetes.csv")






```

```{r percentage with hypertension among diabetics}


#filter
both_noob_nosmo <- filter(dhs_nomiss, ex_diab_broad_ind==1)

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c( ex_htn_broad_ind_dbl))

prevtotall <- svy_treated %>%
summarize(htn_prop = survey_mean(ex_htn_broad_ind_dbl, proportion=TRUE, vartype = "ci")*100))


write_csv(prevtotall, " percentage that has  htn among diabetics.csv")




```





```{r percentage with diabetes among hypertensive patients}


#filter
both_noob_nosmo <- filter(dhs_nomiss, ex_htn_broad_ind==1)

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c( ex_diab_broad_ind_dbl))

prevtotall <- svy_treated %>%
summarize(htn_prop = survey_mean(ex_diab_broad_ind_dbl, proportion=TRUE, vartype = "ci")*100))


write_csv(prevtotall, " percentage that has  diab among htn patients.csv")




```



```{r missing statistics}


dhs_miss <- filter(dhs, is.na(dhs$ex_glucose_ind)==T |  is.na(dhs$fast)==T  | is.na(dhs$hbg12)==T | is.na(dhs$ex_htn_narrow_ind) == T | is.na(dhs$hypt) == T | is.na(dhs$hypt_med) == T | is.na(dhs$bp_ms) == T)

dhs_miss$fast <- as.factor(dhs_miss$fast)
dhs_miss$ex_htn_broad_ind <- as.factor(dhs_miss$ex_htn_broad_ind)
dhs_miss$tobacco_smoked <- as.factor(dhs_miss$tobacco_smoked)
dhs_miss$tobacco_smokeless <- as.factor(dhs_miss$tobacco_smokeless)


table1names <- c("ex_diab_broad_ind","ex_htn_broad_ind", "age_grpOR",
                 "age_grp_men", "age_grp_women", "fast", "educatnames", "wealth_quintile_rurb_lab", "bmi_group", "tobacco_smoked", "tobacco_smokeless", "marriednames", "urban_lab")




totalwithmiss <- CreateTableOne(vars = table1names, data=dhs_miss, includeNA=T)
total <- print(totalwithmiss)

write.csv(total, file="dhs_nomiss multicascades summary missing.csv")

sexwithmiss <- CreateTableOne(vars = table1names, data=dhs_miss, strata=c("female_lab"), includeNA=T)
sexdhs_nomiss <- print(sexwithmiss)

write.csv(sexdhs_nomiss, file= "dhs_nomiss multicascades summary per sex missing.csv")






```

```{r combined cascade ONE SURVEY DOCUMENT women vs men for p-values}

dhs_nomiss <- dhs_nomiss %>% 
  mutate(sex3 = ifelse(sex==1,1,
                       ifelse(sex==0,2,
                              ifelse(sex==1 | sex==0,3,NA))))


#filter
both_noob_nosmo <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(sex,urban_lab,both_treated_dbl, both_aware_dbl, both_controlled_dbl))

write_csv(prevtotall, " combined cascade among all women and men.csv")

svychisq(~sex+both_aware_dbl, svy_treated, statistic="Chisq")
svychisq(~sex+both_treated_dbl, svy_treated, statistic="Chisq")
svychisq(~sex+both_controlled_dbl, svy_treated, statistic="Chisq")

svychisq(~urban_lab+both_aware_dbl, svy_treated, statistic="Chisq")
svychisq(~urban_lab+both_treated_dbl, svy_treated, statistic="Chisq")
svychisq(~urban_lab+both_controlled_dbl, svy_treated, statistic="Chisq")



```

```{r combined cascade among all with both diabetes and hypertension}

#filter
both_noob_nosmo <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtotall <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotall, " combined cascade among all.csv")

```


```{r combined cascade among men}

#filter
both_noob_nosmo <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1 & sex==0) 

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtotmen <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotmen, "combined cascade men .csv")

```



```{r combined cascade among women}

#filter
both_noob_nosmo <- filter(dhs_nomiss, (ex_diab_broad_ind==1 & ex_htn_broad_ind==1 & sex==1)) 

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtotwomen <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotwomen, " combined cascade women .csv")

```

```{r barplot  all women men }



Names <- c("Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Aware", "Treated", "Controlled"))
No_smob <- c(mean(prevtotall$both_aware_prop),mean(prevtotall$both_treated_prop),mean(prevtotall$both_controlled_prop))
One_smob <- c(mean(prevtotwomen$both_aware_prop),mean(prevtotwomen$both_treated_prop),mean(prevtotwomen$both_controlled_prop))
Two_smob  <- c(mean(prevtotmen$both_aware_prop),mean(prevtotmen$both_treated_prop),mean(prevtotmen$both_controlled_prop))


cascades <- data.frame(Names,No_smob, One_smob, Two_smob)


# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(mean(prevtotall$both_aware_prop_low),mean(prevtotall$both_treated_prop_low),mean(prevtotall$both_controlled_prop_low),mean(prevtotwomen$both_aware_prop_low),mean(prevtotwomen$both_treated_prop_low),mean(prevtotwomen$both_controlled_prop_low),mean(prevtotmen$both_aware_prop_low),mean(prevtotmen$both_treated_prop_low),mean(prevtotmen$both_controlled_prop_low))

uppci2 <- c(mean(prevtotall$both_aware_prop_upp),mean(prevtotall$both_treated_prop_upp),mean(prevtotall$both_controlled_prop_upp),mean(prevtotwomen$both_aware_prop_upp),mean(prevtotwomen$both_treated_prop_upp),mean(prevtotwomen$both_controlled_prop_upp),mean(prevtotmen$both_aware_prop_upp),mean(prevtotmen$both_treated_prop_upp),mean(prevtotmen$both_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
     
write.csv(cascades.melt, "combined care all women men .csv")                                                                                     

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "Reds") +
theme_classic() +
  labs(x = "Combined Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brigdiabess("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)








```


```{r barplot  all women men mit 100% bar}



Names <- c("Co-morbid", "Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Co-morbid", "Aware", "Treated", "Controlled"))
No_smob <- c(100,mean(prevtotall$both_aware_prop),mean(prevtotall$both_treated_prop),mean(prevtotall$both_controlled_prop))
One_smob <- c(100,mean(prevtotwomen$both_aware_prop),mean(prevtotwomen$both_treated_prop),mean(prevtotwomen$both_controlled_prop))
Two_smob  <- c(100,mean(prevtotmen$both_aware_prop),mean(prevtotmen$both_treated_prop),mean(prevtotmen$both_controlled_prop))


cascades <- data.frame(Names,No_smob, One_smob, Two_smob)


# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(100,mean(prevtotall$both_aware_prop_low),mean(prevtotall$both_treated_prop_low),mean(prevtotall$both_controlled_prop_low),100,mean(prevtotwomen$both_aware_prop_low),mean(prevtotwomen$both_treated_prop_low),mean(prevtotwomen$both_controlled_prop_low),100,mean(prevtotmen$both_aware_prop_low),mean(prevtotmen$both_treated_prop_low),mean(prevtotmen$both_controlled_prop_low))

uppci2 <- c(100,mean(prevtotall$both_aware_prop_upp),mean(prevtotall$both_treated_prop_upp),mean(prevtotall$both_controlled_prop_upp),100,mean(prevtotwomen$both_aware_prop_upp),mean(prevtotwomen$both_treated_prop_upp),mean(prevtotwomen$both_controlled_prop_upp),100,mean(prevtotmen$both_aware_prop_upp),mean(prevtotmen$both_treated_prop_upp),mean(prevtotmen$both_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
     
write.csv(cascades.melt, "combined care all women men .csv")                                                                                     

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "Reds") +
theme_classic() +
  labs(x = "",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brigdiabess("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)








```



```{r combined cascade among urban}

#filter
both_noob_nosmo <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1 & urban==1) 

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtoturban <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtoturban, " combined cascade urban .csv")

```



```{r combined cascade among rural}

#filter
both_noob_nosmo <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1 & urban==0) 

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtotrural <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotrural, " combined cascade rural .csv")

```



```{r barplot  all urban rural}



Names <- c("Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Aware", "Treated", "Controlled"))
No_smob <- c(mean(prevtotall$both_aware_prop),mean(prevtotall$both_treated_prop),mean(prevtotall$both_controlled_prop))
One_smob <- c(mean(prevtoturban$both_aware_prop),mean(prevtoturban$both_treated_prop),mean(prevtoturban$both_controlled_prop))
Two_smob  <- c(mean(prevtotrural$both_aware_prop),mean(prevtotrural$both_treated_prop),mean(prevtotrural$both_controlled_prop))


cascades <- data.frame(Names,No_smob, One_smob, Two_smob)


# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(mean(prevtotall$both_aware_prop_low),mean(prevtotall$both_treated_prop_low),mean(prevtotall$both_controlled_prop_low),mean(prevtoturban$both_aware_prop_low),mean(prevtoturban$both_treated_prop_low),mean(prevtoturban$both_controlled_prop_low),mean(prevtotrural$both_aware_prop_low),mean(prevtotrural$both_treated_prop_low),mean(prevtotrural$both_controlled_prop_low))

uppci2 <- c(mean(prevtotall$both_aware_prop_upp),mean(prevtotall$both_treated_prop_upp),mean(prevtotall$both_controlled_prop_upp),mean(prevtoturban$both_aware_prop_upp),mean(prevtoturban$both_treated_prop_upp),mean(prevtoturban$both_controlled_prop_upp),mean(prevtotrural$both_aware_prop_upp),mean(prevtotrural$both_treated_prop_upp),mean(prevtotrural$both_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
     
write.csv(cascades.melt, "combined cascade urban rural .csv")                                                                                     

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "Blue") +
theme_classic() +
  labs(x = "Combined Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brigdiabess("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)








```




```{r barplot  all urban rural 100% bar}



Names <- c("Co-morbid", "Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Co-morbid","Aware", "Treated", "Controlled"))
No_smob <- c(100,mean(prevtotall$both_aware_prop),mean(prevtotall$both_treated_prop),mean(prevtotall$both_controlled_prop))
One_smob <- c(100,mean(prevtoturban$both_aware_prop),mean(prevtoturban$both_treated_prop),mean(prevtoturban$both_controlled_prop))
Two_smob  <- c(100,mean(prevtotrural$both_aware_prop),mean(prevtotrural$both_treated_prop),mean(prevtotrural$both_controlled_prop))


cascades <- data.frame(Names,No_smob, One_smob, Two_smob)


# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(100,mean(prevtotall$both_aware_prop_low),mean(prevtotall$both_treated_prop_low),mean(prevtotall$both_controlled_prop_low),100,mean(prevtoturban$both_aware_prop_low),mean(prevtoturban$both_treated_prop_low),mean(prevtoturban$both_controlled_prop_low),100,mean(prevtotrural$both_aware_prop_low),mean(prevtotrural$both_treated_prop_low),mean(prevtotrural$both_controlled_prop_low))

uppci2 <- c(100,mean(prevtotall$both_aware_prop_upp),mean(prevtotall$both_treated_prop_upp),mean(prevtotall$both_controlled_prop_upp),100,mean(prevtoturban$both_aware_prop_upp),mean(prevtoturban$both_treated_prop_upp),mean(prevtoturban$both_controlled_prop_upp),100,mean(prevtotrural$both_aware_prop_upp),mean(prevtotrural$both_treated_prop_upp),mean(prevtotrural$both_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
     
write.csv(cascades.melt, "combined cascade urban rural .csv")                                                                                     

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "Blue") +
theme_classic() +
  labs(x = "",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brigdiabess("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)








```





```{r all three treated}
#filter
both_noob_nosmo <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1 & tobacco_smoked==1)

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(three_treated))

threetreated <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(three_treated, proportion=TRUE, vartype = "ci")*100) 


write_csv(threetreated, " three treated .csv")







```




```{r Combined cascade cvd}

#filter
both_noob_nosmo <- filter(dhs_nomiss, (ex_diab_broad_ind)==1 & ex_htn_broad_ind==1 & obese==0 & tobacco_smoked==0) 

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtotnosmob <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotnosmob, " no ob no smo .csv")







{r htndiab one smob }
#filter
one_of_smob <- filter(dhs_nomiss, (ex_diab_broad_ind)==1 & ex_htn_broad_ind==1 & sum_smob==1)




###create PSU ID in one_of_smob :
one_of_smob <- one_of_smob %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
one_of_smob$psuid <- as.factor(one_of_smob$psuid)

###make noNAinpsu

one_of_smob_noNAinpsu <- filter(one_of_smob, is.na(psu)==F)

summary(one_of_smob_noNAinpsu$psu)

sum(is.na(one_of_smob_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- one_of_smob_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtotonesmob <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotonesmob, " one_of_smob.csv")




{r htndiab two smob}


#filter
both_smob <- filter(dhs_nomiss, (ex_diab_broad_ind)==1 &ex_htn_broad_ind==1 & sum_smob==2)


###create PSU ID in both_smob :
both_smob <- both_smob %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_smob$psuid <- as.factor(both_smob$psuid)

###make noNAinpsu

both_smob_noNAinpsu <- filter(both_smob, is.na(psu)==F)

summary(both_smob_noNAinpsu$psu)

sum(is.na(both_smob_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_smob_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtottwosmob <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtottwosmob, " both_smob.csv")



Names <- c("Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Aware", "Treated", "Controlled"))
No_smob <- c(mean(prevtotnosmob$both_aware_prop),mean(prevtotnosmob$both_treated_prop),mean(prevtotnosmob$both_controlled_prop))
One_smob <- c(mean(prevtotonesmob$both_aware_prop),mean(prevtotonesmob$both_treated_prop),mean(prevtotonesmob$both_controlled_prop))
Two_smob  <- c(mean(prevtottwosmob$both_aware_prop),mean(prevtottwosmob$both_treated_prop),mean(prevtottwosmob$both_controlled_prop))


cascades <- data.frame(Names,No_smob, One_smob, Two_smob)


# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(mean(prevtotnosmob$both_aware_prop_low),mean(prevtotnosmob$both_treated_prop_low),mean(prevtotnosmob$both_controlled_prop_low),mean(prevtotonesmob$both_aware_prop_low),mean(prevtotonesmob$both_treated_prop_low),mean(prevtotonesmob$both_controlled_prop_low),mean(prevtottwosmob$both_aware_prop_low),mean(prevtottwosmob$both_treated_prop_low),mean(prevtottwosmob$both_controlled_prop_low))

uppci2 <- c(mean(prevtotnosmob$both_aware_prop_upp),mean(prevtotnosmob$both_treated_prop_upp),mean(prevtotnosmob$both_controlled_prop_upp),mean(prevtotonesmob$both_aware_prop_upp),mean(prevtotonesmob$both_treated_prop_upp),mean(prevtotonesmob$both_controlled_prop_upp),mean(prevtottwosmob$both_aware_prop_upp),mean(prevtottwosmob$both_treated_prop_upp),mean(prevtottwosmob$both_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
     
write.csv(cascades.melt, "diabetes care summary .csv")                                                                                     

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "Reds") +
theme_classic() +
  labs(x = "Combined Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brigdiabess("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)








```


```{r barplot combined cascades cvd 100% bar}




Names <- c("Co-morbid", "Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Co-morbid","Aware", "Treated", "Controlled"))
No_smob <- c(100,mean(prevtotnosmob$both_aware_prop),mean(prevtotnosmob$both_treated_prop),mean(prevtotnosmob$both_controlled_prop))
One_smob <- c(100,mean(prevtotonesmob$both_aware_prop),mean(prevtotonesmob$both_treated_prop),mean(prevtotonesmob$both_controlled_prop))
Two_smob  <- c(100,mean(prevtottwosmob$both_aware_prop),mean(prevtottwosmob$both_treated_prop),mean(prevtottwosmob$both_controlled_prop))


cascades <- data.frame(Names,No_smob, One_smob, Two_smob)


# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(100,mean(prevtotnosmob$both_aware_prop_low),mean(prevtotnosmob$both_treated_prop_low),mean(prevtotnosmob$both_controlled_prop_low),100,mean(prevtotonesmob$both_aware_prop_low),mean(prevtotonesmob$both_treated_prop_low),mean(prevtotonesmob$both_controlled_prop_low),100,mean(prevtottwosmob$both_aware_prop_low),mean(prevtottwosmob$both_treated_prop_low),mean(prevtottwosmob$both_controlled_prop_low))

uppci2 <- c(100,mean(prevtotnosmob$both_aware_prop_upp),mean(prevtotnosmob$both_treated_prop_upp),mean(prevtotnosmob$both_controlled_prop_upp),100,mean(prevtotonesmob$both_aware_prop_upp),mean(prevtotonesmob$both_treated_prop_upp),mean(prevtotonesmob$both_controlled_prop_upp),100,mean(prevtottwosmob$both_aware_prop_upp),mean(prevtottwosmob$both_treated_prop_upp),mean(prevtottwosmob$both_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
     
write.csv(cascades.melt, "diabetes care summary .csv")                                                                                     

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "Reds") +
theme_classic() +
  labs(x = "Combined Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brigdiabess("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)







```


```{r ANOVAS COMBINED CASCADES COMORBIDITIES}
#filter
both_noob_nosmo <- filter(dhs_nomiss, (ex_diab_broad_ind)==1 & ex_htn_broad_ind==1 )

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(sum_nocvd,sum_smob,both_treated_dbl, both_aware_dbl, both_controlled_dbl))


####CVD comorbidity

interceptonlymodel <- svyglm(both_aware_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(both_aware_dbl~1+sum_smob, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(both_treated_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(both_treated_dbl~1+sum_smob, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(both_controlled_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(both_controlled_dbl~1+sum_smob, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

## NON cvd comorbidity



interceptonlymodel <- svyglm(both_aware_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(both_aware_dbl~1+sum_nocvd, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(both_treated_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(both_treated_dbl~1+sum_nocvd, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(both_controlled_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(both_controlled_dbl~1+sum_nocvd, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")




```


```{r combined cascades non-cvd}

#filter
both_noob_nosmo <- filter(dhs_nomiss, (ex_diab_broad_ind)==1 & ex_htn_broad_ind==1 & sum_nocvd==0)

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtotnosmob <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotnosmob, " no ob no smo .csv")







{r htndiab one noncvd }
#filter
one_of_smob <- filter(dhs_nomiss, (ex_diab_broad_ind)==1 & ex_htn_broad_ind==1 & sum_nocvd==1)




###create PSU ID in one_of_smob :
one_of_smob <- one_of_smob %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
one_of_smob$psuid <- as.factor(one_of_smob$psuid)

###make noNAinpsu

one_of_smob_noNAinpsu <- filter(one_of_smob, is.na(psu)==F)

summary(one_of_smob_noNAinpsu$psu)

sum(is.na(one_of_smob_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- one_of_smob_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtotonesmob <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotonesmob, " one_of_smob.csv")




{r htndiab two noncvd}


#filter
both_smob <- filter(dhs_nomiss, (ex_diab_broad_ind)==1 &ex_htn_broad_ind==1 & sum_nocvd==2)


###create PSU ID in both_smob :
both_smob <- both_smob %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_smob$psuid <- as.factor(both_smob$psuid)

###make noNAinpsu

both_smob_noNAinpsu <- filter(both_smob, is.na(psu)==F)

summary(both_smob_noNAinpsu$psu)

sum(is.na(both_smob_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_smob_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtottwosmob <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtottwosmob, " both_smob.csv")







Names <- c("Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Aware", "Treated", "Controlled"))
No_smob <- c(mean(prevtotnosmob$both_aware_prop),mean(prevtotnosmob$both_treated_prop),mean(prevtotnosmob$both_controlled_prop))
One_smob <- c(mean(prevtotonesmob$both_aware_prop),mean(prevtotonesmob$both_treated_prop),mean(prevtotonesmob$both_controlled_prop))
Two_smob  <- c(mean(prevtottwosmob$both_aware_prop),mean(prevtottwosmob$both_treated_prop),mean(prevtottwosmob$both_controlled_prop))


cascades <- data.frame(Names,No_smob, One_smob, Two_smob)


# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(mean(prevtotnosmob$both_aware_prop_low),mean(prevtotnosmob$both_treated_prop_low),mean(prevtotnosmob$both_controlled_prop_low),mean(prevtotonesmob$both_aware_prop_low),mean(prevtotonesmob$both_treated_prop_low),mean(prevtotonesmob$both_controlled_prop_low),mean(prevtottwosmob$both_aware_prop_low),mean(prevtottwosmob$both_treated_prop_low),mean(prevtottwosmob$both_controlled_prop_low))

uppci2 <- c(mean(prevtotnosmob$both_aware_prop_upp),mean(prevtotnosmob$both_treated_prop_upp),mean(prevtotnosmob$both_controlled_prop_upp),mean(prevtotonesmob$both_aware_prop_upp),mean(prevtotonesmob$both_treated_prop_upp),mean(prevtotonesmob$both_controlled_prop_upp),mean(prevtottwosmob$both_aware_prop_upp),mean(prevtottwosmob$both_treated_prop_upp),mean(prevtottwosmob$both_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
     
write.csv(cascades.melt, "combined cascade number non cvd .csv")                                                                                     

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "PuBu") +
theme_classic() +
  labs(x = "Combined Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brigdiabess("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)



```



```{r barplot combined cascade non cvd 100% bar}



Names <- c("Co-morbid","Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Co-morbid","Aware", "Treated", "Controlled"))
No_smob <- c(100,mean(prevtotnosmob$both_aware_prop),mean(prevtotnosmob$both_treated_prop),mean(prevtotnosmob$both_controlled_prop))
One_smob <- c(100,mean(prevtotonesmob$both_aware_prop),mean(prevtotonesmob$both_treated_prop),mean(prevtotonesmob$both_controlled_prop))
Two_smob  <- c(100,mean(prevtottwosmob$both_aware_prop),mean(prevtottwosmob$both_treated_prop),mean(prevtottwosmob$both_controlled_prop))


cascades <- data.frame(Names,No_smob, One_smob, Two_smob)


# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(100,mean(prevtotnosmob$both_aware_prop_low),mean(prevtotnosmob$both_treated_prop_low),mean(prevtotnosmob$both_controlled_prop_low),100,mean(prevtotonesmob$both_aware_prop_low),mean(prevtotonesmob$both_treated_prop_low),mean(prevtotonesmob$both_controlled_prop_low),100,mean(prevtottwosmob$both_aware_prop_low),mean(prevtottwosmob$both_treated_prop_low),mean(prevtottwosmob$both_controlled_prop_low))

uppci2 <- c(100,mean(prevtotnosmob$both_aware_prop_upp),mean(prevtotnosmob$both_treated_prop_upp),mean(prevtotnosmob$both_controlled_prop_upp),100,mean(prevtotonesmob$both_aware_prop_upp),mean(prevtotonesmob$both_treated_prop_upp),mean(prevtotonesmob$both_controlled_prop_upp),100,mean(prevtottwosmob$both_aware_prop_upp),mean(prevtottwosmob$both_treated_prop_upp),mean(prevtottwosmob$both_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
     
write.csv(cascades.melt, "combined cascade number non cvd .csv")                                                                                     

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "PuBu") +
theme_classic() +
  labs(x = "Combined Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brigdiabess("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)



```




```{r advice no cvd comorb}


#filter
both_smob <- filter(dhs_nomiss, (sum_advice_cvd==0))


###create PSU ID in both_smob :
both_smob <- both_smob %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_smob$psuid <- as.factor(both_smob$psuid)

###make noNAinpsu

both_smob_noNAinpsu <- filter(both_smob, is.na(psu)==F)

summary(both_smob_noNAinpsu$psu)

sum(is.na(both_smob_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_smob_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(tobacco_received_advice_stop))

prevtottwosmob <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(tobacco_received_advice_stop, proportion=TRUE, vartype = "ci")*100)


write_csv(prevtottwosmob, " both_smob.csv")




```







```{r diabetes cascade cvd comorbidities ONE SYRVEY DOCUMENT}

library(survey)
#filter
dhs_nomiss_diabetic <- filter(dhs_nomiss, (ex_diab_broad_ind)==1)


###create PSU ID in dhs_nomiss_diabetic_zerocomorb :
dhs_nomiss_diabetic_zerocomorb <- dhs_nomiss_diabetic%>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_diabetic_zerocomorb$psuid <- as.factor(dhs_nomiss_diabetic_zerocomorb$psuid)

###make noNAinpsu

dhs_nomiss_diabetic_zerocomorb_noNAinpsu <- filter(dhs_nomiss_diabetic_zerocomorb, is.na(psu)==F)

summary(dhs_nomiss_diabetic_zerocomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_diabetic_zerocomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_diabetic_zerocomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(sum_multidiab,diabetic_treated_dbl, diabetic_aware_dbl, diabetic_controlled_dbl))

interceptonlymodel <- svyglm(diabetic_aware_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(diabetic_aware_dbl~1+sum_multidiab, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(diabetic_treated_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(diabetic_treated_dbl~1+sum_multidiab, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(diabetic_controlled_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(diabetic_controlled_dbl~1+sum_multidiab, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")




```



```{r diabetes cascade non-cvd comorbidities ONE SYRVEY DOCUMENT}

library(survey)
#filter
dhs_nomiss_diabetic <- filter(dhs_nomiss, (ex_diab_broad_ind)==1)


###create PSU ID in dhs_nomiss_diabetic_zerocomorb :
dhs_nomiss_diabetic_zerocomorb <- dhs_nomiss_diabetic%>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_diabetic_zerocomorb$psuid <- as.factor(dhs_nomiss_diabetic_zerocomorb$psuid)

###make noNAinpsu

dhs_nomiss_diabetic_zerocomorb_noNAinpsu <- filter(dhs_nomiss_diabetic_zerocomorb, is.na(psu)==F)

summary(dhs_nomiss_diabetic_zerocomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_diabetic_zerocomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_diabetic_zerocomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(sum_nocvd,diabetic_treated_dbl, diabetic_aware_dbl, diabetic_controlled_dbl))

interceptonlymodel <- svyglm(diabetic_aware_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(diabetic_aware_dbl~1+sum_nocvd, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(diabetic_treated_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(diabetic_treated_dbl~1+sum_nocvd, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(diabetic_controlled_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(diabetic_controlled_dbl~1+sum_nocvd, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")




```



```{r diabetes cascade zero cvd comorbidities ####################}


#filter
dhs_nomiss_diabetic <- filter(dhs_nomiss, (ex_diab_broad_ind)==1)
dhs_nomiss_diabetic_zerocomorb <- filter(dhs_nomiss_diabetic,  sum_multidiab==0)


###create PSU ID in dhs_nomiss_diabetic_zerocomorb :
dhs_nomiss_diabetic_zerocomorb <- dhs_nomiss_diabetic_zerocomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_diabetic_zerocomorb$psuid <- as.factor(dhs_nomiss_diabetic_zerocomorb$psuid)

###make noNAinpsu

dhs_nomiss_diabetic_zerocomorb_noNAinpsu <- filter(dhs_nomiss_diabetic_zerocomorb, is.na(psu)==F)

summary(dhs_nomiss_diabetic_zerocomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_diabetic_zerocomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_diabetic_zerocomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_treated_dbl, diabetic_aware_dbl, diabetic_controlled_dbl))

prevtotdiabzerocomorb <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(diabetic_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            diab_treated_prop = survey_mean(diabetic_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            diab_controlled_prop = survey_mean(diabetic_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotdiabzerocomorb, "diabetes cascade dhs_nomiss_diabetic_zerocomorb.csv")




```


```{r diabetes cascade with one cvd comorbidities##########################################}


#filter
dhs_nomiss_diabetic <- filter(dhs_nomiss, (ex_diab_broad_ind)==1)
dhs_nomiss_diabetic_onecomorb <- filter(dhs_nomiss_diabetic,  sum_multidiab==1)


###create PSU ID in dhs_nomiss_diabetic_onecomorb :
dhs_nomiss_diabetic_onecomorb <- dhs_nomiss_diabetic_onecomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_diabetic_onecomorb$psuid <- as.factor(dhs_nomiss_diabetic_onecomorb$psuid)

###make noNAinpsu

dhs_nomiss_diabetic_onecomorb_noNAinpsu <- filter(dhs_nomiss_diabetic_onecomorb, is.na(psu)==F)

summary(dhs_nomiss_diabetic_onecomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_diabetic_onecomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_diabetic_onecomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_treated_dbl, diabetic_aware_dbl, diabetic_controlled_dbl))

prevtotdiabonecomorb <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(diabetic_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            diab_treated_prop = survey_mean(diabetic_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            diab_controlled_prop = survey_mean(diabetic_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotdiabonecomorb, "diabetes cascade dhs_nomiss_diabetic_onecomorb.csv")




```


```{r diabetes cascade with two cvd comorbidities####################################}


#filter
dhs_nomiss_diabetic <- filter(dhs_nomiss, (ex_diab_broad_ind)==1)
dhs_nomiss_diabetic_twocomorb <- filter(dhs_nomiss_diabetic,  sum_multidiab==2)


###create PSU ID in dhs_nomiss_diabetic_twocomorb :
dhs_nomiss_diabetic_twocomorb <- dhs_nomiss_diabetic_twocomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_diabetic_twocomorb$psuid <- as.factor(dhs_nomiss_diabetic_twocomorb$psuid)

###make noNAinpsu

dhs_nomiss_diabetic_twocomorb_noNAinpsu <- filter(dhs_nomiss_diabetic_twocomorb, is.na(psu)==F)

summary(dhs_nomiss_diabetic_twocomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_diabetic_twocomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_diabetic_twocomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_treated_dbl, diabetic_aware_dbl, diabetic_controlled_dbl))

prevtotdiabtwocomorb <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(diabetic_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            diab_treated_prop = survey_mean(diabetic_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            diab_controlled_prop = survey_mean(diabetic_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotdiabtwocomorb, "diabetes cascade dhs_nomiss_diabetic_twocomorb.csv")




```



```{r diabetes cascade with three cvd comorbidities##################}


#filter
dhs_nomiss_diabetic <- filter(dhs_nomiss, (ex_diab_broad_ind)==1)
dhs_nomiss_diabetic_threecomorb <- filter(dhs_nomiss_diabetic,  sum_multidiab==3)


###create PSU ID in dhs_nomiss_diabetic_threecomorb :
dhs_nomiss_diabetic_threecomorb <- dhs_nomiss_diabetic_threecomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_diabetic_threecomorb$psuid <- as.factor(dhs_nomiss_diabetic_threecomorb$psuid)

###make noNAinpsu

dhs_nomiss_diabetic_threecomorb_noNAinpsu <- filter(dhs_nomiss_diabetic_threecomorb, is.na(psu)==F)

summary(dhs_nomiss_diabetic_threecomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_diabetic_threecomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_diabetic_threecomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_treated_dbl, diabetic_aware_dbl, diabetic_controlled_dbl))

prevtotdiabthreecomorb <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(diabetic_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            diab_treated_prop = survey_mean(diabetic_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            diab_controlled_prop = survey_mean(diabetic_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotdiabthreecomorb, "diabetes cascade dhs_nomiss_diabetic_threecomorb.csv")




```



```{r cascades barplot diabetes with number cvd comorbidities}


Names <- c("Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Aware", "Treated", "Controlled"))
No_Comorbidity <- c(mean(prevtotdiabzerocomorb$diab_aware_prop),mean(prevtotdiabzerocomorb$diab_treated_prop),mean(prevtotdiabzerocomorb$diab_controlled_prop))
One_Comorbidity <- c(mean(prevtotdiabonecomorb$diab_aware_prop),mean(prevtotdiabonecomorb$diab_treated_prop),mean(prevtotdiabonecomorb$diab_controlled_prop))
Two_Comorbidities  <- c(mean(prevtotdiabtwocomorb$diab_aware_prop),mean(prevtotdiabtwocomorb$diab_treated_prop),mean(prevtotdiabtwocomorb$diab_controlled_prop))

Three_Comorbidities  <- c(mean(prevtotdiabthreecomorb$diab_aware_prop),mean(prevtotdiabthreecomorb$diab_treated_prop),mean(prevtotdiabthreecomorb$diab_controlled_prop))

cascades <- data.frame(Names,No_Comorbidity, One_Comorbidity, Two_Comorbidities, Three_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(mean(prevtotdiabzerocomorb$diab_aware_prop_low),mean(prevtotdiabzerocomorb$diab_treated_prop_low),mean(prevtotdiabzerocomorb$diab_controlled_prop_low),mean(prevtotdiabonecomorb$diab_aware_prop_low),mean(prevtotdiabonecomorb$diab_treated_prop_low),mean(prevtotdiabonecomorb$diab_controlled_prop_low),mean(prevtotdiabtwocomorb$diab_aware_prop_low),mean(prevtotdiabtwocomorb$diab_treated_prop_low),mean(prevtotdiabtwocomorb$diab_controlled_prop_low), mean(prevtotdiabthreecomorb$diab_aware_prop_low),mean(prevtotdiabthreecomorb$diab_treated_prop_low),mean(prevtotdiabthreecomorb$diab_controlled_prop_low))

uppci2 <- c(mean(prevtotdiabzerocomorb$diab_aware_prop_upp),mean(prevtotdiabzerocomorb$diab_treated_prop_upp),mean(prevtotdiabzerocomorb$diab_controlled_prop_upp),mean(prevtotdiabonecomorb$diab_aware_prop_upp),mean(prevtotdiabonecomorb$diab_treated_prop_upp),mean(prevtotdiabonecomorb$diab_controlled_prop_upp),mean(prevtotdiabtwocomorb$diab_aware_prop_upp),mean(prevtotdiabtwocomorb$diab_treated_prop_upp),mean(prevtotdiabtwocomorb$diab_controlled_prop_upp),mean(prevtotdiabthreecomorb$diab_aware_prop_upp),mean(prevtotdiabthreecomorb$diab_treated_prop_upp),mean(prevtotdiabthreecomorb$diab_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
                                                                                          
write.csv(cascades.melt, "diabetes care number cvd comorbidities .csv") 

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "Reds") +
theme_classic() +
  labs(x = "Diabetes Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brigdiabess("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)



```




```{r cascades barplot diabetes with number cvd comorbidities 100% bar}


Names <- c("Diabetes","Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Diabetes","Aware", "Treated", "Controlled"))
No_Comorbidity <- c(100,mean(prevtotdiabzerocomorb$diab_aware_prop),mean(prevtotdiabzerocomorb$diab_treated_prop),mean(prevtotdiabzerocomorb$diab_controlled_prop))
One_Comorbidity <- c(100,mean(prevtotdiabonecomorb$diab_aware_prop),mean(prevtotdiabonecomorb$diab_treated_prop),mean(prevtotdiabonecomorb$diab_controlled_prop))
Two_Comorbidities  <- c(100,mean(prevtotdiabtwocomorb$diab_aware_prop),mean(prevtotdiabtwocomorb$diab_treated_prop),mean(prevtotdiabtwocomorb$diab_controlled_prop))

Three_Comorbidities  <- c(100,mean(prevtotdiabthreecomorb$diab_aware_prop),mean(prevtotdiabthreecomorb$diab_treated_prop),mean(prevtotdiabthreecomorb$diab_controlled_prop))

cascades <- data.frame(Names,No_Comorbidity, One_Comorbidity, Two_Comorbidities, Three_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(100,mean(prevtotdiabzerocomorb$diab_aware_prop_low),mean(prevtotdiabzerocomorb$diab_treated_prop_low),mean(prevtotdiabzerocomorb$diab_controlled_prop_low),100,mean(prevtotdiabonecomorb$diab_aware_prop_low),mean(prevtotdiabonecomorb$diab_treated_prop_low),mean(prevtotdiabonecomorb$diab_controlled_prop_low),100,mean(prevtotdiabtwocomorb$diab_aware_prop_low),mean(prevtotdiabtwocomorb$diab_treated_prop_low),mean(prevtotdiabtwocomorb$diab_controlled_prop_low),100, mean(prevtotdiabthreecomorb$diab_aware_prop_low),mean(prevtotdiabthreecomorb$diab_treated_prop_low),mean(prevtotdiabthreecomorb$diab_controlled_prop_low))

uppci2 <- c(100,mean(prevtotdiabzerocomorb$diab_aware_prop_upp),mean(prevtotdiabzerocomorb$diab_treated_prop_upp),mean(prevtotdiabzerocomorb$diab_controlled_prop_upp),100,mean(prevtotdiabonecomorb$diab_aware_prop_upp),mean(prevtotdiabonecomorb$diab_treated_prop_upp),mean(prevtotdiabonecomorb$diab_controlled_prop_upp),100,mean(prevtotdiabtwocomorb$diab_aware_prop_upp),mean(prevtotdiabtwocomorb$diab_treated_prop_upp),mean(prevtotdiabtwocomorb$diab_controlled_prop_upp),100,mean(prevtotdiabthreecomorb$diab_aware_prop_upp),mean(prevtotdiabthreecomorb$diab_treated_prop_upp),mean(prevtotdiabthreecomorb$diab_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
                                                                                          
write.csv(cascades.melt, "diabetes care number cvd comorbidities .csv") 

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "Reds") +
theme_classic() +
  labs(x = "Diabetes Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brigdiabess("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)



```






```{r diabetes cascade with zero nocvd comorbidities}


#filter
dhs_nomiss_diabetic <- filter(dhs_nomiss, (ex_diab_broad_ind)==1)
dhs_nomiss_diabetic_zerotreatedcomorb <- filter(dhs_nomiss_diabetic,  sum_nocvd==0)


###create PSU ID in dhs_nomiss_diabetic_zerotreatedcomorb :
dhs_nomiss_diabetic_zerotreatedcomorb <- dhs_nomiss_diabetic_zerotreatedcomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_diabetic_zerotreatedcomorb$psuid <- as.factor(dhs_nomiss_diabetic_zerotreatedcomorb$psuid)

###make noNAinpsu

dhs_nomiss_diabetic_zerotreatedcomorb_noNAinpsu <- filter(dhs_nomiss_diabetic_zerotreatedcomorb, is.na(psu)==F)

summary(dhs_nomiss_diabetic_zerotreatedcomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_diabetic_zerotreatedcomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_diabetic_zerotreatedcomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_treated_dbl, diabetic_aware_dbl, diabetic_controlled_dbl))

prevtotdiabzerotreatedcomorb <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(diabetic_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            diab_treated_prop = survey_mean(diabetic_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            diab_controlled_prop = survey_mean(diabetic_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotdiabzerotreatedcomorb, "diabetes cascade dhs_nomiss_diabetic_zerotreatedcomorb.csv")




```


```{r diabetes cascade with one nocvd comorbidity}


#filter
dhs_nomiss_diabetic <- filter(dhs_nomiss, (ex_diab_broad_ind)==1)
dhs_nomiss_diabetic_onetreatedcomorb <- filter(dhs_nomiss_diabetic,  sum_nocvd==1)


###create PSU ID in dhs_nomiss_diabetic_onetreatedcomorb :
dhs_nomiss_diabetic_onetreatedcomorb <- dhs_nomiss_diabetic_onetreatedcomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_diabetic_onetreatedcomorb$psuid <- as.factor(dhs_nomiss_diabetic_onetreatedcomorb$psuid)

###make noNAinpsu

dhs_nomiss_diabetic_onetreatedcomorb_noNAinpsu <- filter(dhs_nomiss_diabetic_onetreatedcomorb, is.na(psu)==F)

summary(dhs_nomiss_diabetic_onetreatedcomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_diabetic_onetreatedcomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_diabetic_onetreatedcomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_treated_dbl, diabetic_aware_dbl, diabetic_controlled_dbl))

prevtotdiabonetreatedcomorb <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(diabetic_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            diab_treated_prop = survey_mean(diabetic_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            diab_controlled_prop = survey_mean(diabetic_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotdiabonetreatedcomorb, "diabetes cascade dhs_nomiss_diabetic_onetreatedcomorb.csv")




```


```{r diabetes cascade with two nocvd comorbidities}


#filter
dhs_nomiss_diabetic <- filter(dhs_nomiss, (ex_diab_broad_ind)==1)
dhs_nomiss_diabetic_twotreatedcomorb <- filter(dhs_nomiss_diabetic,  sum_nocvd==2)


###create PSU ID in dhs_nomiss_diabetic_twotreatedcomorb :
dhs_nomiss_diabetic_twotreatedcomorb <- dhs_nomiss_diabetic_twotreatedcomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_diabetic_twotreatedcomorb$psuid <- as.factor(dhs_nomiss_diabetic_twotreatedcomorb$psuid)

###make noNAinpsu

dhs_nomiss_diabetic_twotreatedcomorb_noNAinpsu <- filter(dhs_nomiss_diabetic_twotreatedcomorb, is.na(psu)==F)

summary(dhs_nomiss_diabetic_twotreatedcomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_diabetic_twotreatedcomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_diabetic_twotreatedcomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_treated_dbl, diabetic_aware_dbl, diabetic_controlled_dbl))

prevtotdiabtwotreatedcomorb <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(diabetic_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            diab_treated_prop = survey_mean(diabetic_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            diab_controlled_prop = survey_mean(diabetic_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotdiabtwotreatedcomorb, "diabetes cascade dhs_nomiss_diabetic_twotreatedcomorb.csv")




```




```{r cascades barplot diabetes with number non cvd comorbidities########################}


Names <- c("Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Aware", "Treated", "Controlled"))
No_Comorbidity <- c(mean(prevtotdiabzerotreatedcomorb$diab_aware_prop),mean(prevtotdiabzerotreatedcomorb$diab_treated_prop),mean(prevtotdiabzerotreatedcomorb$diab_controlled_prop))
One_Comorbidity <- c(mean(prevtotdiabonetreatedcomorb$diab_aware_prop),mean(prevtotdiabonetreatedcomorb$diab_treated_prop),mean(prevtotdiabonetreatedcomorb$diab_controlled_prop))
Two_Comorbidities  <- c(mean(prevtotdiabtwotreatedcomorb$diab_aware_prop),mean(prevtotdiabtwotreatedcomorb$diab_treated_prop),mean(prevtotdiabtwotreatedcomorb$diab_controlled_prop))

cascades <- data.frame(Names,No_Comorbidity, One_Comorbidity, Two_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(mean(prevtotdiabzerotreatedcomorb$diab_aware_prop_low),mean(prevtotdiabzerotreatedcomorb$diab_treated_prop_low),mean(prevtotdiabzerotreatedcomorb$diab_controlled_prop_low),mean(prevtotdiabonetreatedcomorb$diab_aware_prop_low),mean(prevtotdiabonetreatedcomorb$diab_treated_prop_low),mean(prevtotdiabonetreatedcomorb$diab_controlled_prop_low),mean(prevtotdiabtwotreatedcomorb$diab_aware_prop_low),mean(prevtotdiabtwotreatedcomorb$diab_treated_prop_low),mean(prevtotdiabtwotreatedcomorb$diab_controlled_prop_low))

uppci2 <- c(mean(prevtotdiabzerotreatedcomorb$diab_aware_prop_upp),mean(prevtotdiabzerotreatedcomorb$diab_treated_prop_upp),mean(prevtotdiabzerotreatedcomorb$diab_controlled_prop_upp),mean(prevtotdiabonetreatedcomorb$diab_aware_prop_upp),mean(prevtotdiabonetreatedcomorb$diab_treated_prop_upp),mean(prevtotdiabonetreatedcomorb$diab_controlled_prop_upp),mean(prevtotdiabtwotreatedcomorb$diab_aware_prop_upp),mean(prevtotdiabtwotreatedcomorb$diab_treated_prop_upp),mean(prevtotdiabtwotreatedcomorb$diab_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
  
write.csv(cascades.melt, "diabetes care number non cvd comorbidities .csv")                                                                                         

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "PuBu") +
theme_classic() +
  labs(x = "Diabetes Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)



```




```{r cascades barplot diabetes with number non cvd comorbidities with 100% bar########################}


Names <- c("Diabetes","Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Diabetes","Aware", "Treated", "Controlled"))
No_Comorbidity <- c(100,mean(prevtotdiabzerotreatedcomorb$diab_aware_prop),mean(prevtotdiabzerotreatedcomorb$diab_treated_prop),mean(prevtotdiabzerotreatedcomorb$diab_controlled_prop))
One_Comorbidity <- c(100,mean(prevtotdiabonetreatedcomorb$diab_aware_prop),mean(prevtotdiabonetreatedcomorb$diab_treated_prop),mean(prevtotdiabonetreatedcomorb$diab_controlled_prop))
Two_Comorbidities  <- c(100,mean(prevtotdiabtwotreatedcomorb$diab_aware_prop),mean(prevtotdiabtwotreatedcomorb$diab_treated_prop),mean(prevtotdiabtwotreatedcomorb$diab_controlled_prop))

cascades <- data.frame(Names,No_Comorbidity, One_Comorbidity, Two_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(100,mean(prevtotdiabzerotreatedcomorb$diab_aware_prop_low),mean(prevtotdiabzerotreatedcomorb$diab_treated_prop_low),mean(prevtotdiabzerotreatedcomorb$diab_controlled_prop_low),100,mean(prevtotdiabonetreatedcomorb$diab_aware_prop_low),mean(prevtotdiabonetreatedcomorb$diab_treated_prop_low),mean(prevtotdiabonetreatedcomorb$diab_controlled_prop_low),100,mean(prevtotdiabtwotreatedcomorb$diab_aware_prop_low),mean(prevtotdiabtwotreatedcomorb$diab_treated_prop_low),mean(prevtotdiabtwotreatedcomorb$diab_controlled_prop_low))

uppci2 <- c(100,mean(prevtotdiabzerotreatedcomorb$diab_aware_prop_upp),mean(prevtotdiabzerotreatedcomorb$diab_treated_prop_upp),mean(prevtotdiabzerotreatedcomorb$diab_controlled_prop_upp),100,mean(prevtotdiabonetreatedcomorb$diab_aware_prop_upp),mean(prevtotdiabonetreatedcomorb$diab_treated_prop_upp),mean(prevtotdiabonetreatedcomorb$diab_controlled_prop_upp),100,mean(prevtotdiabtwotreatedcomorb$diab_aware_prop_upp),mean(prevtotdiabtwotreatedcomorb$diab_treated_prop_upp),mean(prevtotdiabtwotreatedcomorb$diab_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
  
write.csv(cascades.melt, "diabetes care number non cvd comorbidities .csv")                                                                                         

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "PuBu") +
theme_classic() +
  labs(x = "Diabetes Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)



```





```{r diabetes cascade with asthma but not anemia}


#filter
dhs_nomiss_diabetic <- filter(dhs_nomiss, (ex_diab_broad_ind)==1)
dhs_nomiss_diabetic_asthmabutnotanemia <- filter(dhs_nomiss_diabetic,  anemia==0 & asthma==1)


###create PSU ID in dhs_nomiss_diabetic_asthmabutnotanemia :
dhs_nomiss_diabetic_asthmabutnotanemia <- dhs_nomiss_diabetic_asthmabutnotanemia %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_diabetic_asthmabutnotanemia$psuid <- as.factor(dhs_nomiss_diabetic_asthmabutnotanemia$psuid)

###make noNAinpsu

dhs_nomiss_diabetic_asthmabutnotanemia_noNAinpsu <- filter(dhs_nomiss_diabetic_asthmabutnotanemia, is.na(psu)==F)

summary(dhs_nomiss_diabetic_asthmabutnotanemia_noNAinpsu$psu)

sum(is.na(dhs_nomiss_diabetic_asthmabutnotanemia_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_diabetic_asthmabutnotanemia_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_treated_dbl, diabetic_aware_dbl, diabetic_controlled_dbl))

prevtotdiabasthmabutnotanemia <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(diabetic_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            diab_treated_prop = survey_mean(diabetic_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            diab_controlled_prop = survey_mean(diabetic_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotdiabasthmabutnotanemia, "diabetes cascade dhs_nomiss_diabetic_asthmabutnotanemia.csv")




```


```{r diabetes cascade with anemia but not asthma}


#filter
dhs_nomiss_diabetic <- filter(dhs_nomiss, (ex_diab_broad_ind)==1)
dhs_nomiss_diabetic_anemiabutnotasthma <- filter(dhs_nomiss_diabetic,  anemia==1 & asthma==0)


###create PSU ID in dhs_nomiss_diabetic_anemiabutnotasthma :
dhs_nomiss_diabetic_anemiabutnotasthma <- dhs_nomiss_diabetic_anemiabutnotasthma %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_diabetic_anemiabutnotasthma$psuid <- as.factor(dhs_nomiss_diabetic_anemiabutnotasthma$psuid)

###make noNAinpsu

dhs_nomiss_diabetic_anemiabutnotasthma_noNAinpsu <- filter(dhs_nomiss_diabetic_anemiabutnotasthma, is.na(psu)==F)

summary(dhs_nomiss_diabetic_anemiabutnotasthma_noNAinpsu$psu)

sum(is.na(dhs_nomiss_diabetic_anemiabutnotasthma_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_diabetic_anemiabutnotasthma_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_treated_dbl, diabetic_aware_dbl, diabetic_controlled_dbl))

prevtotdiabanemiabutnotasthma <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(diabetic_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            diab_treated_prop = survey_mean(diabetic_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            diab_controlled_prop = survey_mean(diabetic_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotdiabanemiabutnotasthma, "diabetes cascade dhs_nomiss_diabetic_anemiabutnotasthma.csv")




```


```{r diabetes cascade with asthma and anemia}


#filter
dhs_nomiss_diabetic <- filter(dhs_nomiss, (ex_diab_broad_ind)==1)
dhs_nomiss_diabetic_twotreatedcomorb <- filter(dhs_nomiss_diabetic,  sum_nocvd==2)


###create PSU ID in dhs_nomiss_diabetic_twotreatedcomorb :
dhs_nomiss_diabetic_twotreatedcomorb <- dhs_nomiss_diabetic_twotreatedcomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_diabetic_twotreatedcomorb$psuid <- as.factor(dhs_nomiss_diabetic_twotreatedcomorb$psuid)

###make noNAinpsu

dhs_nomiss_diabetic_twotreatedcomorb_noNAinpsu <- filter(dhs_nomiss_diabetic_twotreatedcomorb, is.na(psu)==F)

summary(dhs_nomiss_diabetic_twotreatedcomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_diabetic_twotreatedcomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_diabetic_twotreatedcomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_treated_dbl, diabetic_aware_dbl, diabetic_controlled_dbl))

prevtotdiabtwotreatedcomorb <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(diabetic_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            diab_treated_prop = survey_mean(diabetic_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            diab_controlled_prop = survey_mean(diabetic_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotdiabtwotreatedcomorb, "diabetes cascade dhs_nomiss_diabetic_twotreatedcomorb.csv")




```

```{r cascades barplot diabetes with number astha but not anemia, anemia but not asthma with 100% bar########################}


Names <- c("Diabetes","Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Diabetes","Aware", "Treated", "Controlled"))
Anemiabutnotasthma <- c(100,mean(prevtotdiabanemiabutnotasthma$diab_aware_prop),mean(prevtotdiabanemiabutnotasthma$diab_treated_prop),mean(prevtotdiabanemiabutnotasthma$diab_controlled_prop))
Asthmabutnotanemia <- c(100,mean(prevtotdiabasthmabutnotanemia$diab_aware_prop),mean(prevtotdiabasthmabutnotanemia$diab_treated_prop),mean(prevtotdiabasthmabutnotanemia$diab_controlled_prop))
Two_Comorbidities  <- c(100,mean(prevtotdiabtwotreatedcomorb$diab_aware_prop),mean(prevtotdiabtwotreatedcomorb$diab_treated_prop),mean(prevtotdiabtwotreatedcomorb$diab_controlled_prop))

cascades <- data.frame(Names,Anemiabutnotasthma, Asthmabutnotanemia, Two_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(100,mean(prevtotdiabanemiabutnotasthma$diab_aware_prop_low),mean(prevtotdiabanemiabutnotasthma$diab_treated_prop_low),mean(prevtotdiabanemiabutnotasthma$diab_controlled_prop_low),100,mean(prevtotdiabasthmabutnotanemia$diab_aware_prop_low),mean(prevtotdiabasthmabutnotanemia$diab_treated_prop_low),mean(prevtotdiabasthmabutnotanemia$diab_controlled_prop_low),100,mean(prevtotdiabtwotreatedcomorb$diab_aware_prop_low),mean(prevtotdiabtwotreatedcomorb$diab_treated_prop_low),mean(prevtotdiabtwotreatedcomorb$diab_controlled_prop_low))

uppci2 <- c(100,mean(prevtotdiabanemiabutnotasthma$diab_aware_prop_upp),mean(prevtotdiabanemiabutnotasthma$diab_treated_prop_upp),mean(prevtotdiabanemiabutnotasthma$diab_controlled_prop_upp),100,mean(prevtotdiabasthmabutnotanemia$diab_aware_prop_upp),mean(prevtotdiabasthmabutnotanemia$diab_treated_prop_upp),mean(prevtotdiabasthmabutnotanemia$diab_controlled_prop_upp),100,mean(prevtotdiabtwotreatedcomorb$diab_aware_prop_upp),mean(prevtotdiabtwotreatedcomorb$diab_treated_prop_upp),mean(prevtotdiabtwotreatedcomorb$diab_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
  
write.csv(cascades.melt, "diabetes care number non cvd comorbidities .csv")                                                                                         

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "PuBu") +
theme_classic() +
  labs(x = "Diabetes Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)



```




```{r htn cascade with asthma but not anemia and anemia but not asthma###############################################}


#filter
dhs_nomiss_htn <- filter(dhs_nomiss, (ex_htn_broad_ind)==1)
dhs_nomiss_htn_zerotreatedcomorb <- filter(dhs_nomiss_htn,  asthma==0 & anemia ==1)


###create PSU ID in dhs_nomiss_htn_zerotreatedcomorb :
dhs_nomiss_htn_zerotreatedcomorb <- dhs_nomiss_htn_zerotreatedcomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_htn_zerotreatedcomorb$psuid <- as.factor(dhs_nomiss_htn_zerotreatedcomorb$psuid)

###make noNAinpsu

dhs_nomiss_htn_zerotreatedcomorb_noNAinpsu <- filter(dhs_nomiss_htn_zerotreatedcomorb, is.na(psu)==F)

summary(dhs_nomiss_htn_zerotreatedcomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_htn_zerotreatedcomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_htn_zerotreatedcomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_screened_dbl, htn_treated_dbl, htn_aware_dbl, htn_controlled_dbl))

prevtothtnzerotreatedcomorb <- svy_treated %>%
  summarize(htn_screened_prop = survey_mean(htn_screened_dbl, proportion=TRUE,  vartype = "ci")*100, htn_aware_prop = survey_mean(htn_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            htn_treated_prop = survey_mean(htn_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            htn_controlled_prop = survey_mean(htn_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtothtnzerotreatedcomorb, "htn cascade dhs_nomiss_htn_zerotreatedcomorb.csv")





#filter
dhs_nomiss_htn <- filter(dhs_nomiss, (ex_htn_broad_ind)==1)
dhs_nomiss_htn_onetreatedcomorb <- filter(dhs_nomiss_htn,  asthma==1 & anemia==0)


###create PSU ID in dhs_nomiss_htn_onetreatedcomorb :
dhs_nomiss_htn_onetreatedcomorb <- dhs_nomiss_htn_onetreatedcomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_htn_onetreatedcomorb$psuid <- as.factor(dhs_nomiss_htn_onetreatedcomorb$psuid)

###make noNAinpsu

dhs_nomiss_htn_onetreatedcomorb_noNAinpsu <- filter(dhs_nomiss_htn_onetreatedcomorb, is.na(psu)==F)

summary(dhs_nomiss_htn_onetreatedcomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_htn_onetreatedcomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_htn_onetreatedcomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_screened_dbl, htn_treated_dbl, htn_aware_dbl, htn_controlled_dbl))

prevtothtnonetreatedcomorb <- svy_treated %>%
  summarize(htn_screened_prop = survey_mean(htn_screened_dbl, proportion=TRUE,  vartype = "ci")*100, htn_aware_prop = survey_mean(htn_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            htn_treated_prop = survey_mean(htn_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            htn_controlled_prop = survey_mean(htn_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtothtnonetreatedcomorb, "htn cascade dhs_nomiss_htn_onetreatedcomorb.csv")




#filter
dhs_nomiss_htn <- filter(dhs_nomiss, (ex_htn_broad_ind)==1)
dhs_nomiss_htn_twotreatedcomorb <- filter(dhs_nomiss_htn,  sum_nocvd==2)


###create PSU ID in dhs_nomiss_htn_twotreatedcomorb :
dhs_nomiss_htn_twotreatedcomorb <- dhs_nomiss_htn_twotreatedcomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_htn_twotreatedcomorb$psuid <- as.factor(dhs_nomiss_htn_twotreatedcomorb$psuid)

###make noNAinpsu

dhs_nomiss_htn_twotreatedcomorb_noNAinpsu <- filter(dhs_nomiss_htn_twotreatedcomorb, is.na(psu)==F)

summary(dhs_nomiss_htn_twotreatedcomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_htn_twotreatedcomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_htn_twotreatedcomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_screened_dbl, htn_treated_dbl, htn_aware_dbl, htn_controlled_dbl))

prevtothtntwotreatedcomorb <- svy_treated %>%
  summarize(htn_screened_prop = survey_mean(htn_screened_dbl, proportion=TRUE,  vartype = "ci")*100, htn_aware_prop = survey_mean(htn_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            htn_treated_prop = survey_mean(htn_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            htn_controlled_prop = survey_mean(htn_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtothtntwotreatedcomorb, "htn cascade dhs_nomiss_htn_twotreatedcomorb.csv")










{r cascades barplot Hypertension with number noncvd comorbidities#################}


Names <- c("Screened", "Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Screened", "Aware", "Treated", "Controlled"))
Anemianoasthma <- c(mean(prevtothtnzerotreatedcomorb$htn_screened_prop),mean(prevtothtnzerotreatedcomorb$htn_aware_prop),mean(prevtothtnzerotreatedcomorb$htn_treated_prop),mean(prevtothtnzerotreatedcomorb$htn_controlled_prop))
Asthmanoanemia <- c(mean(prevtothtnonetreatedcomorb$htn_screened_prop),mean(prevtothtnonetreatedcomorb$htn_aware_prop),mean(prevtothtnonetreatedcomorb$htn_treated_prop),mean(prevtothtnonetreatedcomorb$htn_controlled_prop))
Two_Comorbidities  <- c(mean(prevtothtntwotreatedcomorb$htn_screened_prop),mean(prevtothtntwotreatedcomorb$htn_aware_prop),mean(prevtothtntwotreatedcomorb$htn_treated_prop),mean(prevtothtntwotreatedcomorb$htn_controlled_prop))

cascades <- data.frame(Names,Anemianoasthma, Asthmanoanemia, Two_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(mean(prevtothtnzerotreatedcomorb$htn_screened_prop_low),mean(prevtothtnzerotreatedcomorb$htn_aware_prop_low),mean(prevtothtnzerotreatedcomorb$htn_treated_prop_low),mean(prevtothtnzerotreatedcomorb$htn_controlled_prop_low),mean(prevtothtnonetreatedcomorb$htn_screened_prop_low),mean(prevtothtnonetreatedcomorb$htn_aware_prop_low),mean(prevtothtnonetreatedcomorb$htn_treated_prop_low),mean(prevtothtnonetreatedcomorb$htn_controlled_prop_low),mean(prevtothtntwotreatedcomorb$htn_screened_prop_low),mean(prevtothtntwotreatedcomorb$htn_aware_prop_low),mean(prevtothtntwotreatedcomorb$htn_treated_prop_low),mean(prevtothtntwotreatedcomorb$htn_controlled_prop_low))

uppci2 <- c(mean(prevtothtnzerotreatedcomorb$htn_screened_prop_upp),mean(prevtothtnzerotreatedcomorb$htn_aware_prop_upp),mean(prevtothtnzerotreatedcomorb$htn_treated_prop_upp),mean(prevtothtnzerotreatedcomorb$htn_controlled_prop_upp),mean(prevtothtnonetreatedcomorb$htn_screened_prop_upp),mean(prevtothtnonetreatedcomorb$htn_aware_prop_upp),mean(prevtothtnonetreatedcomorb$htn_treated_prop_upp),mean(prevtothtnonetreatedcomorb$htn_controlled_prop_upp),mean(prevtothtntwotreatedcomorb$htn_screened_prop_upp),mean(prevtothtntwotreatedcomorb$htn_aware_prop_upp),mean(prevtothtntwotreatedcomorb$htn_treated_prop_upp),mean(prevtothtntwotreatedcomorb$htn_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
                                                                                          
write.csv(cascades.melt, "hypertension cascades number non cvd comorbidities .csv") 


# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "PuBu") +
theme_classic() +
  labs(x = "Hypertension Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(4/100, expand=F)



```



```{r combined cascades non-cvd}

#filter
both_noob_nosmo <- filter(dhs_nomiss, (ex_diab_broad_ind)==1 & ex_htn_broad_ind==1 & asthma==0 & anemia==1)

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_noob_nosmo_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtotnosmob <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotnosmob, " no ob no smo .csv")







{r htndiab one noncvd }
#filter
one_of_smob <- filter(dhs_nomiss, (ex_diab_broad_ind)==1 & ex_htn_broad_ind==1 & asthma==1 & anemia==0)




###create PSU ID in one_of_smob :
one_of_smob <- one_of_smob %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
one_of_smob$psuid <- as.factor(one_of_smob$psuid)

###make noNAinpsu

one_of_smob_noNAinpsu <- filter(one_of_smob, is.na(psu)==F)

summary(one_of_smob_noNAinpsu$psu)

sum(is.na(one_of_smob_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- one_of_smob_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtotonesmob <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtotonesmob, " one_of_smob.csv")




{r htndiab two noncvd}


#filter
both_smob <- filter(dhs_nomiss, (ex_diab_broad_ind)==1 &ex_htn_broad_ind==1 & sum_nocvd==2)


###create PSU ID in both_smob :
both_smob <- both_smob %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_smob$psuid <- as.factor(both_smob$psuid)

###make noNAinpsu

both_smob_noNAinpsu <- filter(both_smob, is.na(psu)==F)

summary(both_smob_noNAinpsu$psu)

sum(is.na(both_smob_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- both_smob_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, both_aware_dbl, both_controlled_dbl))

prevtottwosmob <- svy_treated %>%
  summarize(both_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            both_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            both_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtottwosmob, " both_smob.csv")




```



```{r barplot combined cascade non cvd 100% bar}



Names <- c("Co-morbid","Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Co-morbid","Aware", "Treated", "Controlled"))
Anemianoasthma <- c(100,mean(prevtotnosmob$both_aware_prop),mean(prevtotnosmob$both_treated_prop),mean(prevtotnosmob$both_controlled_prop))
Asthmanoanemia <- c(100,mean(prevtotonesmob$both_aware_prop),mean(prevtotonesmob$both_treated_prop),mean(prevtotonesmob$both_controlled_prop))
Two_smob  <- c(100,mean(prevtottwosmob$both_aware_prop),mean(prevtottwosmob$both_treated_prop),mean(prevtottwosmob$both_controlled_prop))


cascades <- data.frame(Names,Anemianoasthma, Asthmanoanemia, Two_smob)


# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(100,mean(prevtotnosmob$both_aware_prop_low),mean(prevtotnosmob$both_treated_prop_low),mean(prevtotnosmob$both_controlled_prop_low),100,mean(prevtotonesmob$both_aware_prop_low),mean(prevtotonesmob$both_treated_prop_low),mean(prevtotonesmob$both_controlled_prop_low),100,mean(prevtottwosmob$both_aware_prop_low),mean(prevtottwosmob$both_treated_prop_low),mean(prevtottwosmob$both_controlled_prop_low))

uppci2 <- c(100,mean(prevtotnosmob$both_aware_prop_upp),mean(prevtotnosmob$both_treated_prop_upp),mean(prevtotnosmob$both_controlled_prop_upp),100,mean(prevtotonesmob$both_aware_prop_upp),mean(prevtotonesmob$both_treated_prop_upp),mean(prevtotonesmob$both_controlled_prop_upp),100,mean(prevtottwosmob$both_aware_prop_upp),mean(prevtottwosmob$both_treated_prop_upp),mean(prevtottwosmob$both_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
     
write.csv(cascades.melt, "combined cascade number non cvd .csv")                                                                                     

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "PuBu") +
theme_classic() +
  labs(x = "Combined Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brigdiabess("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(3/100, expand=F)



```




```{r advice with zero cvd ####################}


#filter
dhs_nomiss_advice <- filter(dhs_nomiss,sum_advice_cvd==0)

###create PSU ID in dhs_nomiss_diabetic_zerocomorb :
dhs_nomiss_advice <- dhs_nomiss_advice %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_advice$psuid <- as.factor(dhs_nomiss_advice$psuid)

###make noNAinpsu

dhs_nomiss_advice_noNAinpsu <- filter(dhs_nomiss_advice, is.na(psu)==F)

summary(dhs_nomiss_advice_noNAinpsu$psu)

sum(is.na(dhs_nomiss_advice_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_advice_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(tobacco_received_advice_stop))

prevtotdiabzerocomorb <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(tobacco_received_advice_stop, proportion=TRUE, vartype = "ci")*100)


write_csv(prevtotdiabzerocomorb, "diabetes cascade dhs_nomiss_advice.csv")




```


```{r advice with one cvd##########################################}


#filter
dhs_nomiss_advice_one <- filter(dhs_nomiss,sum_advice_cvd==1)


###create PSU ID in dhs_nomiss_advice :
dhs_nomiss_advice_one <- dhs_nomiss_advice_one %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_advice_one$psuid <- as.factor(dhs_nomiss_advice_one$psuid)

###make noNAinpsu

dhs_nomiss_advice_one_noNAinpsu <- filter(dhs_nomiss_advice_one, is.na(psu)==F)

summary(dhs_nomiss_advice_one_noNAinpsu$psu)

sum(is.na(dhs_nomiss_advice_one_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_advice_one_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(tobacco_received_advice_stop))

prevtotdiabonecomorb <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(tobacco_received_advice_stop, proportion=TRUE, vartype = "ci")*100)


write_csv(prevtotdiabonecomorb, "diabetes cascade dhs_nomiss_advice_one.csv")




```


```{r advice with two cvd####################################}


#filter
dhs_nomiss_advice_two <- filter(dhs_nomiss,sum_advice_cvd==2)


###create PSU ID in dhs_nomiss_advice_two :
dhs_nomiss_advice_two <- dhs_nomiss_advice_two %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_advice_two$psuid <- as.factor(dhs_nomiss_advice_two$psuid)

###make noNAinpsu

dhs_nomiss_advice_two_noNAinpsu <- filter(dhs_nomiss_advice_two, is.na(psu)==F)

summary(dhs_nomiss_advice_two_noNAinpsu$psu)

sum(is.na(dhs_nomiss_advice_two_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_advice_two_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(tobacco_received_advice_stop))

prevtotdiabtwocomorb <- svy_treated %>%
 summarize(diab_aware_prop = survey_mean(tobacco_received_advice_stop, proportion=TRUE, vartype = "ci")*100)

write_csv(prevtotdiabtwocomorb, "diabetes cascade dhs_nomiss_advice_two.csv")




```



```{r advice with three cvd##################}


#filter
dhs_nomiss_advice_three <- filter(dhs_nomiss,sum_advice_cvd==3)

###create PSU ID in dhs_nomiss_diabetic_threecomorb :
dhs_nomiss_advice_three <- dhs_nomiss_advice_three %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_advice_three$psuid <- as.factor(dhs_nomiss_advice_three$psuid)

###make noNAinpsu

dhs_nomiss_advice_three_noNAinpsu <- filter(dhs_nomiss_advice_three, is.na(psu)==F)

summary(dhs_nomiss_advice_three_noNAinpsu$psu)

sum(is.na(dhs_nomiss_advice_three_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_advice_three_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(tobacco_received_advice_stop))

prevtotdiabthreecomorb <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(tobacco_received_advice_stop, proportion=TRUE, vartype = "ci")*100)


write_csv(prevtotdiabthreecomorb, "diabetes cascade dhs_nomiss_advice_three.csv")




```


```{r barplot advice cvd}


Names <- c("Treated")
Names <- factor(Names, levels = c("Treated"))
No_Comorbidity <- c(mean(prevtotdiabzerocomorb$diab_aware_prop))
One_Comorbidity <- c(mean(prevtotdiabonecomorb$diab_aware_prop))
Two_Comorbidities  <- c(mean(prevtotdiabtwocomorb$diab_aware_prop))

Three_Comorbidities  <- c(mean(prevtotdiabthreecomorb$diab_aware_prop))

cascades <- data.frame(Names,No_Comorbidity, One_Comorbidity, Two_Comorbidities, Three_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(mean(prevtotdiabzerocomorb$diab_aware_prop_low),mean(prevtotdiabonecomorb$diab_aware_prop_low),mean(prevtotdiabtwocomorb$diab_aware_prop_low),mean(prevtotdiabthreecomorb$diab_aware_prop_low))

uppci2 <- c(mean(prevtotdiabzerocomorb$diab_aware_prop_upp),mean(prevtotdiabonecomorb$diab_aware_prop_upp),mean(prevtotdiabtwocomorb$diab_aware_prop_upp),mean(prevtotdiabthreecomorb$diab_aware_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
                                                                                          
write.csv(cascades.melt, "advice cvd comorbidities .csv") 

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "Reds") +
theme_classic() +
  labs(x = "Tobacco smoking",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brigdiabess("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(1/100, expand=F)



```


```{r barplot advice cvd with 100%bar}


Names <- c("Smoking","Treated")
Names <- factor(Names, levels = c("Smoking","Treated"))
No_Comorbidity <- c(100,mean(prevtotdiabzerocomorb$diab_aware_prop))
One_Comorbidity <- c(100,mean(prevtotdiabonecomorb$diab_aware_prop))
Two_Comorbidities  <- c(100,mean(prevtotdiabtwocomorb$diab_aware_prop))

Three_Comorbidities  <- c(100,mean(prevtotdiabthreecomorb$diab_aware_prop))

cascades <- data.frame(Names,No_Comorbidity, One_Comorbidity, Two_Comorbidities, Three_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(100,mean(prevtotdiabzerocomorb$diab_aware_prop_low),mean(prevtotdiabonecomorb$diab_aware_prop_low),mean(prevtotdiabtwocomorb$diab_aware_prop_low),mean(prevtotdiabthreecomorb$diab_aware_prop_low))

uppci2 <- c(100,mean(prevtotdiabzerocomorb$diab_aware_prop_upp),mean(prevtotdiabonecomorb$diab_aware_prop_upp),mean(prevtotdiabtwocomorb$diab_aware_prop_upp),mean(prevtotdiabthreecomorb$diab_aware_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
                                                                                          
write.csv(cascades.melt, "advice cvd comorbidities .csv") 

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "Reds") +
theme_classic() +
  labs(x = "Tobacco smoking",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brigdiabess("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(1/100, expand=F)



```



```{r advice with zero noncvd}


#filter
dhs_nomiss_advice_zero_no <- filter(dhs_nomiss, sum_nocvd==0)


###create PSU ID in dhs_nomiss_advice_zero_no :
dhs_nomiss_advice_zero_no <- dhs_nomiss_advice_zero_no %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_advice_zero_no$psuid <- as.factor(dhs_nomiss_advice_zero_no$psuid)

###make noNAinpsu

dhs_nomiss_advice_zero_no_noNAinpsu <- filter(dhs_nomiss_advice_zero_no, is.na(psu)==F)

summary(dhs_nomiss_advice_zero_no_noNAinpsu$psu)

sum(is.na(dhs_nomiss_advice_zero_no_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_advice_zero_no_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(tobacco_received_advice_stop))

prevtotdiabzerotreatedcomorb <- svy_treated %>%
 summarize(diab_aware_prop = survey_mean(tobacco_received_advice_stop, proportion=TRUE, vartype = "ci")*100)


write_csv(prevtotdiabzerotreatedcomorb, "diabetes cascade dhs_nomiss_advice_zero_no.csv")




```


```{r advice with one noncvd}


#filter
dhs_nomiss_advice_one_no <- filter(dhs_nomiss, sum_nocvd==1)


###create PSU ID in dhs_nomiss_advice_one_no :
dhs_nomiss_advice_one_no <- dhs_nomiss_advice_one_no %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_advice_one_no$psuid <- as.factor(dhs_nomiss_advice_one_no$psuid)

###make noNAinpsu

dhs_nomiss_advice_one_no_noNAinpsu <- filter(dhs_nomiss_advice_one_no, is.na(psu)==F)

summary(dhs_nomiss_advice_one_no_noNAinpsu$psu)

sum(is.na(dhs_nomiss_advice_one_no_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_advice_one_no_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(tobacco_received_advice_stop))

prevtotdiabonetreatedcomorb <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(tobacco_received_advice_stop, proportion=TRUE, vartype = "ci")*100)


write_csv(prevtotdiabonetreatedcomorb, "diabetes cascade dhs_nomiss_advice_one_no.csv")




```


```{r advice with two noncvd}


#filter
dhs_nomiss_advice_two_no <- filter(dhs_nomiss,  sum_nocvd==2)


###create PSU ID in dhs_nomiss_diabetic_twotreatedcomorb :
dhs_nomiss_advice_two_no <- dhs_nomiss_advice_two_no %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_advice_two_no$psuid <- as.factor(dhs_nomiss_advice_two_no$psuid)

###make noNAinpsu

dhs_nomiss_advice_two_no_noNAinpsu <- filter(dhs_nomiss_advice_two_no, is.na(psu)==F)

summary(dhs_nomiss_advice_two_no_noNAinpsu$psu)

sum(is.na(dhs_nomiss_advice_two_no_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_advice_two_no_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(tobacco_received_advice_stop))

prevtotdiabtwotreatedcomorb <- svy_treated %>%
  summarize(diab_aware_prop = survey_mean(tobacco_received_advice_stop, proportion=TRUE, vartype = "ci")*100)


write_csv(prevtotdiabtwotreatedcomorb, "diabetes cascade dhs_nomiss_advice_two_no.csv")




```


```{r barplot advice non cvd}

Names <- c( "Treated")
Names <- factor(Names, levels = c("Treated"))
No_Comorbidity <- c(mean(prevtotdiabzerotreatedcomorb$diab_aware_prop))
One_Comorbidity <- c(mean(prevtotdiabonetreatedcomorb$diab_aware_prop))
Two_Comorbidities  <- c(mean(prevtotdiabtwotreatedcomorb$diab_aware_prop))

cascades <- data.frame(Names,No_Comorbidity, One_Comorbidity, Two_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(mean(prevtotdiabzerotreatedcomorb$diab_aware_prop_low),mean(prevtotdiabonetreatedcomorb$diab_aware_prop_low),mean(prevtotdiabtwotreatedcomorb$diab_aware_prop_low))

uppci2 <- c(mean(prevtotdiabzerotreatedcomorb$diab_aware_prop_upp),mean(prevtotdiabonetreatedcomorb$diab_aware_prop_upp),mean(prevtotdiabtwotreatedcomorb$diab_aware_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
  
write.csv(cascades.melt, "advice non cvd comorbidities .csv")                                                                                         

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "PuBu") +
theme_classic() +
  labs(x = "Tobacco smoking",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(1/100, expand=F)



```



```{r barplot advice non cvd 100% bar plot}

Names <- c( "Smoking","Treated")
Names <- factor(Names, levels = c( "Smoking","Treated"))
No_Comorbidity <- c(100,mean(prevtotdiabzerotreatedcomorb$diab_aware_prop))
One_Comorbidity <- c(100,mean(prevtotdiabonetreatedcomorb$diab_aware_prop))
Two_Comorbidities  <- c(100,mean(prevtotdiabtwotreatedcomorb$diab_aware_prop))

cascades <- data.frame(Names,No_Comorbidity, One_Comorbidity, Two_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(100,mean(prevtotdiabzerotreatedcomorb$diab_aware_prop_low),mean(prevtotdiabonetreatedcomorb$diab_aware_prop_low),mean(prevtotdiabtwotreatedcomorb$diab_aware_prop_low))

uppci2 <- c(100,mean(prevtotdiabzerotreatedcomorb$diab_aware_prop_upp),mean(prevtotdiabonetreatedcomorb$diab_aware_prop_upp),mean(prevtotdiabtwotreatedcomorb$diab_aware_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
  
write.csv(cascades.melt, "advice non cvd comorbidities .csv")                                                                                         

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "PuBu") +
theme_classic() +
  labs(x = "Tobacco smoking",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(1/100, expand=F)



```










```{r htn cascade cvd#########################################}


#filter
dhs_nomiss_htn <- filter(dhs_nomiss, (ex_htn_broad_ind)==1)
dhs_nomiss_htn_zerocomorb <- filter(dhs_nomiss_htn,  sum_multihtn==0)


###create PSU ID in dhs_nomiss_htn_zerocomorb :
dhs_nomiss_htn_zerocomorb <- dhs_nomiss_htn_zerocomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_htn_zerocomorb$psuid <- as.factor(dhs_nomiss_htn_zerocomorb$psuid)

###make noNAinpsu

dhs_nomiss_htn_zerocomorb_noNAinpsu <- filter(dhs_nomiss_htn_zerocomorb, is.na(psu)==F)

summary(dhs_nomiss_htn_zerocomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_htn_zerocomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_htn_zerocomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_screened_dbl, htn_treated_dbl, htn_aware_dbl, htn_controlled_dbl))

prevtothtnzerocomorb <- svy_treated %>%
  summarize(htn_screened_prop = survey_mean(htn_screened_dbl, proportion=TRUE,  vartype = "ci")*100, htn_aware_prop = survey_mean(htn_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            htn_treated_prop = survey_mean(htn_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            htn_controlled_prop = survey_mean(htn_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtothtnzerocomorb, "htn cascade dhs_nomiss_htn_zerocomorb.csv")







{r htn cascade with one comorbidities}


#filter
dhs_nomiss_htn <- filter(dhs_nomiss, (ex_htn_broad_ind)==1)
dhs_nomiss_htn_onecomorb <- filter(dhs_nomiss_htn,  sum_multihtn==1)


###create PSU ID in dhs_nomiss_htn_onecomorb :
dhs_nomiss_htn_onecomorb <- dhs_nomiss_htn_onecomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_htn_onecomorb$psuid <- as.factor(dhs_nomiss_htn_onecomorb$psuid)

###make noNAinpsu

dhs_nomiss_htn_onecomorb_noNAinpsu <- filter(dhs_nomiss_htn_onecomorb, is.na(psu)==F)

summary(dhs_nomiss_htn_onecomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_htn_onecomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_htn_onecomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_screened_dbl, htn_treated_dbl, htn_aware_dbl, htn_controlled_dbl))

prevtothtnonecomorb <- svy_treated %>%
  summarize(htn_screened_prop = survey_mean(htn_screened_dbl, proportion=TRUE,  vartype = "ci")*100, htn_aware_prop = survey_mean(htn_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            htn_treated_prop = survey_mean(htn_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            htn_controlled_prop = survey_mean(htn_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtothtnonecomorb, "htn cascade dhs_nomiss_htn_onecomorb.csv")







{r htn cascade with two comorbidities}


#filter
dhs_nomiss_htn <- filter(dhs_nomiss, (ex_htn_broad_ind)==1)
dhs_nomiss_htn_twocomorb <- filter(dhs_nomiss_htn,  sum_multihtn==2)


###create PSU ID in dhs_nomiss_htn_twocomorb :
dhs_nomiss_htn_twocomorb <- dhs_nomiss_htn_twocomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_htn_twocomorb$psuid <- as.factor(dhs_nomiss_htn_twocomorb$psuid)

###make noNAinpsu

dhs_nomiss_htn_twocomorb_noNAinpsu <- filter(dhs_nomiss_htn_twocomorb, is.na(psu)==F)

summary(dhs_nomiss_htn_twocomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_htn_twocomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_htn_twocomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_screened_dbl, htn_treated_dbl, htn_aware_dbl, htn_controlled_dbl))

prevtothtntwocomorb <- svy_treated %>%
  summarize(htn_screened_prop = survey_mean(htn_screened_dbl, proportion=TRUE,  vartype = "ci")*100, htn_aware_prop = survey_mean(htn_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            htn_treated_prop = survey_mean(htn_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            htn_controlled_prop = survey_mean(htn_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtothtntwocomorb, "htn cascade dhs_nomiss_htn_twocomorb.csv")








{r htn cascade with three comorbidities}


#filter
dhs_nomiss_htn <- filter(dhs_nomiss, (ex_htn_broad_ind)==1)
dhs_nomiss_htn_threecomorb <- filter(dhs_nomiss_htn,  sum_multihtn==3)


###create PSU ID in dhs_nomiss_htn_threecomorb :
dhs_nomiss_htn_threecomorb <- dhs_nomiss_htn_threecomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_htn_threecomorb$psuid <- as.factor(dhs_nomiss_htn_threecomorb$psuid)

###make noNAinpsu

dhs_nomiss_htn_threecomorb_noNAinpsu <- filter(dhs_nomiss_htn_threecomorb, is.na(psu)==F)

summary(dhs_nomiss_htn_threecomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_htn_threecomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_htn_threecomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_screened_dbl, htn_treated_dbl, htn_aware_dbl, htn_controlled_dbl))

prevtothtnthreecomorb <- svy_treated %>%
  summarize(htn_screened_prop = survey_mean(htn_screened_dbl, proportion=TRUE,  vartype = "ci")*100, htn_aware_prop = survey_mean(htn_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            htn_treated_prop = survey_mean(htn_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            htn_controlled_prop = survey_mean(htn_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtothtnthreecomorb, "htn cascade dhs_nomiss_htn_threecomorb.csv")








{r cascades barplot Hypertension with number comorbidities}


Names <- c("Screened", "Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Screened", "Aware", "Treated", "Controlled"))
No_Comorbidity <- c(mean(prevtothtnzerocomorb$htn_screened_prop),mean(prevtothtnzerocomorb$htn_aware_prop),mean(prevtothtnzerocomorb$htn_treated_prop),mean(prevtothtnzerocomorb$htn_controlled_prop))
One_Comorbidity <- c(mean(prevtothtnonecomorb$htn_screened_prop),mean(prevtothtnonecomorb$htn_aware_prop),mean(prevtothtnonecomorb$htn_treated_prop),mean(prevtothtnonecomorb$htn_controlled_prop))
Two_Comorbidities  <- c(mean(prevtothtntwocomorb$htn_screened_prop),mean(prevtothtntwocomorb$htn_aware_prop),mean(prevtothtntwocomorb$htn_treated_prop),mean(prevtothtntwocomorb$htn_controlled_prop))

Three_Comorbidities  <- c(mean(prevtothtnthreecomorb$htn_screened_prop),mean(prevtothtnthreecomorb$htn_aware_prop),mean(prevtothtnthreecomorb$htn_treated_prop),mean(prevtothtnthreecomorb$htn_controlled_prop))

cascades <- data.frame(Names,No_Comorbidity, One_Comorbidity, Two_Comorbidities, Three_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(mean(prevtothtnzerocomorb$htn_screened_prop_low),mean(prevtothtnzerocomorb$htn_aware_prop_low),mean(prevtothtnzerocomorb$htn_treated_prop_low),mean(prevtothtnzerocomorb$htn_controlled_prop_low),mean(prevtothtnonecomorb$htn_screened_prop_low),mean(prevtothtnonecomorb$htn_aware_prop_low),mean(prevtothtnonecomorb$htn_treated_prop_low),mean(prevtothtnonecomorb$htn_controlled_prop_low),mean(prevtothtntwocomorb$htn_screened_prop_low),mean(prevtothtntwocomorb$htn_aware_prop_low),mean(prevtothtntwocomorb$htn_treated_prop_low),mean(prevtothtntwocomorb$htn_controlled_prop_low),mean(prevtothtnthreecomorb$htn_screened_prop_low), mean(prevtothtnthreecomorb$htn_aware_prop_low),mean(prevtothtnthreecomorb$htn_treated_prop_low),mean(prevtothtnthreecomorb$htn_controlled_prop_low))

uppci2 <- c(mean(prevtothtnzerocomorb$htn_screened_prop_upp),mean(prevtothtnzerocomorb$htn_aware_prop_upp),mean(prevtothtnzerocomorb$htn_treated_prop_upp),mean(prevtothtnzerocomorb$htn_controlled_prop_upp),mean(prevtothtnonecomorb$htn_screened_prop_upp),mean(prevtothtnonecomorb$htn_aware_prop_upp),mean(prevtothtnonecomorb$htn_treated_prop_upp),mean(prevtothtnonecomorb$htn_controlled_prop_upp), mean(prevtothtntwocomorb$htn_screened_prop_upp), mean(prevtothtntwocomorb$htn_aware_prop_upp),mean(prevtothtntwocomorb$htn_treated_prop_upp),mean(prevtothtntwocomorb$htn_controlled_prop_upp),mean(prevtothtnthreecomorb$htn_screened_prop_upp), mean(prevtothtnthreecomorb$htn_aware_prop_upp),mean(prevtothtnthreecomorb$htn_treated_prop_upp),mean(prevtothtnthreecomorb$htn_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
 

write.csv(cascades.melt, "hypertension cascades number cvd comorbidities .csv")                                                                                          

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "Reds") +
theme_classic() +
  labs(x = "Hypertension Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(4/100, expand=F)



```



```{r Hypertension cascade ONE SURVEY DOCUMENT p-values}

#filter
dhs_nomiss_htn_zerocomorb <- filter(dhs_nomiss, (ex_htn_broad_ind)==1)



###create PSU ID in dhs_nomiss_htn_zerocomorb :
dhs_nomiss_htn_zerocomorb <- dhs_nomiss_htn_zerocomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_htn_zerocomorb$psuid <- as.factor(dhs_nomiss_htn_zerocomorb$psuid)

###make noNAinpsu

dhs_nomiss_htn_zerocomorb_noNAinpsu <- filter(dhs_nomiss_htn_zerocomorb, is.na(psu)==F)

summary(dhs_nomiss_htn_zerocomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_htn_zerocomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_htn_zerocomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(sum_nocvd, sum_multihtn, htn_screened_dbl, htn_treated_dbl, htn_aware_dbl, htn_controlled_dbl))

####CVD comorbidity

interceptonlymodel <- svyglm(htn_screened_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(htn_screened_dbl~1+sum_multihtn, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(htn_aware_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(htn_aware_dbl~1+sum_multihtn, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(htn_treated_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(htn_treated_dbl~1+sum_multihtn, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(htn_controlled_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(htn_controlled_dbl~1+sum_multihtn, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

## NON cvd comorbidity


interceptonlymodel <- svyglm(htn_screened_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(htn_screened_dbl~1+sum_nocvd, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(htn_aware_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(htn_aware_dbl~1+sum_nocvd, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(htn_treated_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(htn_treated_dbl~1+sum_nocvd, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")

interceptonlymodel <- svyglm(htn_controlled_dbl~1, design= svy_treated, family=binomial)
modelwriskf <- svyglm(htn_controlled_dbl~1+sum_nocvd, design= svy_treated, family=binomial)

anova(modelwriskf, interceptonlymodel, method="Wald")



```

```{r cascades barplot Hypertension with number cvd comorbidities 100% bar}


Names <- c("Hypertension","Screened", "Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Hypertension","Screened", "Aware", "Treated", "Controlled"))
No_Comorbidity <- c(100,mean(prevtothtnzerocomorb$htn_screened_prop),mean(prevtothtnzerocomorb$htn_aware_prop),mean(prevtothtnzerocomorb$htn_treated_prop),mean(prevtothtnzerocomorb$htn_controlled_prop))
One_Comorbidity <- c(100,mean(prevtothtnonecomorb$htn_screened_prop),mean(prevtothtnonecomorb$htn_aware_prop),mean(prevtothtnonecomorb$htn_treated_prop),mean(prevtothtnonecomorb$htn_controlled_prop))
Two_Comorbidities  <- c(100,mean(prevtothtntwocomorb$htn_screened_prop),mean(prevtothtntwocomorb$htn_aware_prop),mean(prevtothtntwocomorb$htn_treated_prop),mean(prevtothtntwocomorb$htn_controlled_prop))

Three_Comorbidities  <- c(100,mean(prevtothtnthreecomorb$htn_screened_prop),mean(prevtothtnthreecomorb$htn_aware_prop),mean(prevtothtnthreecomorb$htn_treated_prop),mean(prevtothtnthreecomorb$htn_controlled_prop))

cascades <- data.frame(Names,No_Comorbidity, One_Comorbidity, Two_Comorbidities, Three_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(100,mean(prevtothtnzerocomorb$htn_screened_prop_low),mean(prevtothtnzerocomorb$htn_aware_prop_low),mean(prevtothtnzerocomorb$htn_treated_prop_low),mean(prevtothtnzerocomorb$htn_controlled_prop_low),100,mean(prevtothtnonecomorb$htn_screened_prop_low),mean(prevtothtnonecomorb$htn_aware_prop_low),mean(prevtothtnonecomorb$htn_treated_prop_low),mean(prevtothtnonecomorb$htn_controlled_prop_low),100,mean(prevtothtntwocomorb$htn_screened_prop_low),mean(prevtothtntwocomorb$htn_aware_prop_low),mean(prevtothtntwocomorb$htn_treated_prop_low),mean(prevtothtntwocomorb$htn_controlled_prop_low),100,mean(prevtothtnthreecomorb$htn_screened_prop_low), mean(prevtothtnthreecomorb$htn_aware_prop_low),mean(prevtothtnthreecomorb$htn_treated_prop_low),mean(prevtothtnthreecomorb$htn_controlled_prop_low))

uppci2 <- c(100,mean(prevtothtnzerocomorb$htn_screened_prop_upp),mean(prevtothtnzerocomorb$htn_aware_prop_upp),mean(prevtothtnzerocomorb$htn_treated_prop_upp),mean(prevtothtnzerocomorb$htn_controlled_prop_upp),100,mean(prevtothtnonecomorb$htn_screened_prop_upp),mean(prevtothtnonecomorb$htn_aware_prop_upp),mean(prevtothtnonecomorb$htn_treated_prop_upp),mean(prevtothtnonecomorb$htn_controlled_prop_upp),100, mean(prevtothtntwocomorb$htn_screened_prop_upp), mean(prevtothtntwocomorb$htn_aware_prop_upp),mean(prevtothtntwocomorb$htn_treated_prop_upp),mean(prevtothtntwocomorb$htn_controlled_prop_upp),100,mean(prevtothtnthreecomorb$htn_screened_prop_upp), mean(prevtothtnthreecomorb$htn_aware_prop_upp),mean(prevtothtnthreecomorb$htn_treated_prop_upp),mean(prevtothtnthreecomorb$htn_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
 

write.csv(cascades.melt, "hypertension cascades number cvd comorbidities .csv")                                                                                          

# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "Reds") +
theme_classic() +
  labs(x = "Hypertension Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(4/100, expand=F)



```


```{r htn cascade with noncvd###############################################}


#filter
dhs_nomiss_htn <- filter(dhs_nomiss, (ex_htn_broad_ind)==1)
dhs_nomiss_htn_zerotreatedcomorb <- filter(dhs_nomiss_htn,  sum_nocvd==0)


###create PSU ID in dhs_nomiss_htn_zerotreatedcomorb :
dhs_nomiss_htn_zerotreatedcomorb <- dhs_nomiss_htn_zerotreatedcomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_htn_zerotreatedcomorb$psuid <- as.factor(dhs_nomiss_htn_zerotreatedcomorb$psuid)

###make noNAinpsu

dhs_nomiss_htn_zerotreatedcomorb_noNAinpsu <- filter(dhs_nomiss_htn_zerotreatedcomorb, is.na(psu)==F)

summary(dhs_nomiss_htn_zerotreatedcomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_htn_zerotreatedcomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_htn_zerotreatedcomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_screened_dbl, htn_treated_dbl, htn_aware_dbl, htn_controlled_dbl))

prevtothtnzerotreatedcomorb <- svy_treated %>%
  summarize(htn_screened_prop = survey_mean(htn_screened_dbl, proportion=TRUE,  vartype = "ci")*100, htn_aware_prop = survey_mean(htn_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            htn_treated_prop = survey_mean(htn_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            htn_controlled_prop = survey_mean(htn_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtothtnzerotreatedcomorb, "htn cascade dhs_nomiss_htn_zerotreatedcomorb.csv")







{r htn cascade with one noncvd comorbidities}


#filter
dhs_nomiss_htn <- filter(dhs_nomiss, (ex_htn_broad_ind)==1)
dhs_nomiss_htn_onetreatedcomorb <- filter(dhs_nomiss_htn,  sum_nocvd==1)


###create PSU ID in dhs_nomiss_htn_onetreatedcomorb :
dhs_nomiss_htn_onetreatedcomorb <- dhs_nomiss_htn_onetreatedcomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_htn_onetreatedcomorb$psuid <- as.factor(dhs_nomiss_htn_onetreatedcomorb$psuid)

###make noNAinpsu

dhs_nomiss_htn_onetreatedcomorb_noNAinpsu <- filter(dhs_nomiss_htn_onetreatedcomorb, is.na(psu)==F)

summary(dhs_nomiss_htn_onetreatedcomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_htn_onetreatedcomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_htn_onetreatedcomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_screened_dbl, htn_treated_dbl, htn_aware_dbl, htn_controlled_dbl))

prevtothtnonetreatedcomorb <- svy_treated %>%
  summarize(htn_screened_prop = survey_mean(htn_screened_dbl, proportion=TRUE,  vartype = "ci")*100, htn_aware_prop = survey_mean(htn_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            htn_treated_prop = survey_mean(htn_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            htn_controlled_prop = survey_mean(htn_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtothtnonetreatedcomorb, "htn cascade dhs_nomiss_htn_onetreatedcomorb.csv")







{r htn cascade with two noncvd comorbidities}


#filter
dhs_nomiss_htn <- filter(dhs_nomiss, (ex_htn_broad_ind)==1)
dhs_nomiss_htn_twotreatedcomorb <- filter(dhs_nomiss_htn,  sum_nocvd==2)


###create PSU ID in dhs_nomiss_htn_twotreatedcomorb :
dhs_nomiss_htn_twotreatedcomorb <- dhs_nomiss_htn_twotreatedcomorb %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_htn_twotreatedcomorb$psuid <- as.factor(dhs_nomiss_htn_twotreatedcomorb$psuid)

###make noNAinpsu

dhs_nomiss_htn_twotreatedcomorb_noNAinpsu <- filter(dhs_nomiss_htn_twotreatedcomorb, is.na(psu)==F)

summary(dhs_nomiss_htn_twotreatedcomorb_noNAinpsu$psu)

sum(is.na(dhs_nomiss_htn_twotreatedcomorb_noNAinpsu$psu)==T)


#### Crude cascade estimates


svy_treated <- dhs_nomiss_htn_twotreatedcomorb_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_screened_dbl, htn_treated_dbl, htn_aware_dbl, htn_controlled_dbl))

prevtothtntwotreatedcomorb <- svy_treated %>%
  summarize(htn_screened_prop = survey_mean(htn_screened_dbl, proportion=TRUE,  vartype = "ci")*100, htn_aware_prop = survey_mean(htn_aware_dbl, proportion=TRUE, vartype = "ci")*100, 
            htn_treated_prop = survey_mean(htn_treated_dbl, proportion=TRUE, vartype = "ci")*100,
            htn_controlled_prop = survey_mean(htn_controlled_dbl, proportion=TRUE, vartype = "ci")*100) 


write_csv(prevtothtntwotreatedcomorb, "htn cascade dhs_nomiss_htn_twotreatedcomorb.csv")










{r cascades barplot Hypertension with number noncvd comorbidities#################}


Names <- c("Screened", "Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Screened", "Aware", "Treated", "Controlled"))
No_Comorbidity <- c(mean(prevtothtnzerotreatedcomorb$htn_screened_prop),mean(prevtothtnzerotreatedcomorb$htn_aware_prop),mean(prevtothtnzerotreatedcomorb$htn_treated_prop),mean(prevtothtnzerotreatedcomorb$htn_controlled_prop))
One_Comorbidity <- c(mean(prevtothtnonetreatedcomorb$htn_screened_prop),mean(prevtothtnonetreatedcomorb$htn_aware_prop),mean(prevtothtnonetreatedcomorb$htn_treated_prop),mean(prevtothtnonetreatedcomorb$htn_controlled_prop))
Two_Comorbidities  <- c(mean(prevtothtntwotreatedcomorb$htn_screened_prop),mean(prevtothtntwotreatedcomorb$htn_aware_prop),mean(prevtothtntwotreatedcomorb$htn_treated_prop),mean(prevtothtntwotreatedcomorb$htn_controlled_prop))

cascades <- data.frame(Names,No_Comorbidity, One_Comorbidity, Two_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(mean(prevtothtnzerotreatedcomorb$htn_screened_prop_low),mean(prevtothtnzerotreatedcomorb$htn_aware_prop_low),mean(prevtothtnzerotreatedcomorb$htn_treated_prop_low),mean(prevtothtnzerotreatedcomorb$htn_controlled_prop_low),mean(prevtothtnonetreatedcomorb$htn_screened_prop_low),mean(prevtothtnonetreatedcomorb$htn_aware_prop_low),mean(prevtothtnonetreatedcomorb$htn_treated_prop_low),mean(prevtothtnonetreatedcomorb$htn_controlled_prop_low),mean(prevtothtntwotreatedcomorb$htn_screened_prop_low),mean(prevtothtntwotreatedcomorb$htn_aware_prop_low),mean(prevtothtntwotreatedcomorb$htn_treated_prop_low),mean(prevtothtntwotreatedcomorb$htn_controlled_prop_low))

uppci2 <- c(mean(prevtothtnzerotreatedcomorb$htn_screened_prop_upp),mean(prevtothtnzerotreatedcomorb$htn_aware_prop_upp),mean(prevtothtnzerotreatedcomorb$htn_treated_prop_upp),mean(prevtothtnzerotreatedcomorb$htn_controlled_prop_upp),mean(prevtothtnonetreatedcomorb$htn_screened_prop_upp),mean(prevtothtnonetreatedcomorb$htn_aware_prop_upp),mean(prevtothtnonetreatedcomorb$htn_treated_prop_upp),mean(prevtothtnonetreatedcomorb$htn_controlled_prop_upp),mean(prevtothtntwotreatedcomorb$htn_screened_prop_upp),mean(prevtothtntwotreatedcomorb$htn_aware_prop_upp),mean(prevtothtntwotreatedcomorb$htn_treated_prop_upp),mean(prevtothtntwotreatedcomorb$htn_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
                                                                                          
write.csv(cascades.melt, "hypertension cascades number non cvd comorbidities .csv") 


# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "PuBu") +
theme_classic() +
  labs(x = "Hypertension Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(4/100, expand=F)



```




```{r cascades barplot Hypertension with number noncvd comorbidities 100% bar}


Names <- c("Hypertension","Screened", "Aware", "Treated", "Controlled")
Names <- factor(Names, levels = c("Hypertension","Screened", "Aware", "Treated", "Controlled"))
No_Comorbidity <- c(100,mean(prevtothtnzerotreatedcomorb$htn_screened_prop),mean(prevtothtnzerotreatedcomorb$htn_aware_prop),mean(prevtothtnzerotreatedcomorb$htn_treated_prop),mean(prevtothtnzerotreatedcomorb$htn_controlled_prop))
One_Comorbidity <- c(100,mean(prevtothtnonetreatedcomorb$htn_screened_prop),mean(prevtothtnonetreatedcomorb$htn_aware_prop),mean(prevtothtnonetreatedcomorb$htn_treated_prop),mean(prevtothtnonetreatedcomorb$htn_controlled_prop))
Two_Comorbidities  <- c(100,mean(prevtothtntwotreatedcomorb$htn_screened_prop),mean(prevtothtntwotreatedcomorb$htn_aware_prop),mean(prevtothtntwotreatedcomorb$htn_treated_prop),mean(prevtothtntwotreatedcomorb$htn_controlled_prop))

cascades <- data.frame(Names,No_Comorbidity, One_Comorbidity, Two_Comorbidities)



# melt the data frame for plotting
library(reshape)
cascades.melt <- melt(cascades, id.vars='Names')

###add errorbars

lowci2 <- c(100,mean(prevtothtnzerotreatedcomorb$htn_screened_prop_low),mean(prevtothtnzerotreatedcomorb$htn_aware_prop_low),mean(prevtothtnzerotreatedcomorb$htn_treated_prop_low),mean(prevtothtnzerotreatedcomorb$htn_controlled_prop_low),100,mean(prevtothtnonetreatedcomorb$htn_screened_prop_low),mean(prevtothtnonetreatedcomorb$htn_aware_prop_low),mean(prevtothtnonetreatedcomorb$htn_treated_prop_low),mean(prevtothtnonetreatedcomorb$htn_controlled_prop_low),100,mean(prevtothtntwotreatedcomorb$htn_screened_prop_low),mean(prevtothtntwotreatedcomorb$htn_aware_prop_low),mean(prevtothtntwotreatedcomorb$htn_treated_prop_low),mean(prevtothtntwotreatedcomorb$htn_controlled_prop_low))

uppci2 <- c(100,mean(prevtothtnzerotreatedcomorb$htn_screened_prop_upp),mean(prevtothtnzerotreatedcomorb$htn_aware_prop_upp),mean(prevtothtnzerotreatedcomorb$htn_treated_prop_upp),mean(prevtothtnzerotreatedcomorb$htn_controlled_prop_upp),100,mean(prevtothtnonetreatedcomorb$htn_screened_prop_upp),mean(prevtothtnonetreatedcomorb$htn_aware_prop_upp),mean(prevtothtnonetreatedcomorb$htn_treated_prop_upp),mean(prevtothtnonetreatedcomorb$htn_controlled_prop_upp),100,mean(prevtothtntwotreatedcomorb$htn_screened_prop_upp),mean(prevtothtntwotreatedcomorb$htn_aware_prop_upp),mean(prevtothtntwotreatedcomorb$htn_treated_prop_upp),mean(prevtothtntwotreatedcomorb$htn_controlled_prop_upp))
            

cascades.melt <- mutate(cascades.melt,
                        lowci = lowci2,
                        uppci =uppci2)

cascades.melt$Names <- as.factor(cascades.melt$Names)
                                                                                          
write.csv(cascades.melt, "hypertension cascades number non cvd comorbidities .csv") 


# plot everything
ggplot(cascades.melt, aes(Names, value, ymin= lowci, ymax=uppci)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  geom_errorbar(aes(group=factor(variable)), width=.2,position=position_dodge(.9)) +
  scale_fill_brewer(palette = "PuBu") +
theme_classic() +
  labs(x = "Hypertension Care Cascade",
       y = "Percent",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 5)), name="Education") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17), name="Education") +
  scale_y_continuous( limits=c(0, 100)) +
  coord_fixed(4/100, expand=F)



```






```{r multivariable regression both diseases NOW CORRECT###########################################################}

##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)


##Diabetes aware

diabetes_aware <- glm.cluster(formula = both_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))


aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
aware <- as.data.frame(aware, optional = TRUE)

write.csv(aware, "OR diabetes aware .csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes aware.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes aware .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases aware.csv")
 



##Diabetes treated

diabetes_treated <- glm.cluster(formula = both_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "OR diabetes treated .csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treated.csv")



######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes treated .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases treated.csv")
 


##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = both_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "OR diabetes controlled .csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlled.csv")



######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes controlled .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlled.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases controlled.csv")
 

```



```{r multivariable regression combined cascade with obese and smoke}

##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)


##Diabetes aware

diabetes_aware <- glm.cluster(formula = both_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))


aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
aware <- as.data.frame(aware, optional = TRUE)

write.csv(aware, "OR diabetes aware .csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes aware.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes aware .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases aware with obese and smoking.csv")
 



##Diabetes treated

diabetes_treated <- glm.cluster(formula = both_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "OR diabetes treated .csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treated.csv")



######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes treated .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases treated with obese and smoking.csv")
 


##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = both_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "OR diabetes controlled .csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlled.csv")



######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes controlled .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlled.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases controlled with obese and smoking.csv")
 

```


```{r multivariable regressions diabetes cascade with obese and smoke and htn}


##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1)


##diabetes aware 3 cvdrisc

diabetes_aware <- glm.cluster(formula = diabetic_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_htn_broad_ind + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
write.csv(aware, "RR diabetes aware 3 cvdrisc poisson .csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes aware 3 cvdriscpoisson .csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes aware 3 cvdrisc poisson .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware 3 cvdriscpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes aware 3 cvdrisc single disease diabetes aware 3 cvdrisc.csv")
 


##diabetes treated 3 cvdrisc

diabetes_treated <- glm.cluster(formula = diabetic_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_htn_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "RR diabetes treated 3 cvdrisc poisson .csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treated 3 cvdriscpoisson .csv")

######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes treated 3 cvdrisc poisson .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated 3 cvdriscpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes treated 3 cvdrisc single disease diabetes treated 3 cvdrisc.csv")
 



##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = diabetic_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + ex_htn_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "RR diabetes controlled 3 cvdriscpoisson .csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlledpoisson .csv")

######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes controlled 3 cvdriscpoisson .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlledpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes controlled"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes controlled 3 cvdriscsingle disease diabetes controlled.csv")
 




```


```{r multivariable regressions htn cascade with obese and smoke and diabetes}


##filter
dhs_nomiss_htn_only <- filter(dhs_nomiss, ex_htn_broad_ind==1) 

##htn screened 3 cvdrisc

htn_screened <- glm.cluster(formula = htn_screened_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex +  obese + tobacco_smoked + ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

screened <- exp(cbind(RR = coef(htn_screened), confint(htn_screened)))
write.csv(screened, "RR htn screened 3 cvdrisc poisson.csv")

results_screened <-summary(htn_screened)
write.csv(results_screened, "p Value htn screened 3 cvdriscpoisson.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn screened 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn screened 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn screened 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn screened 3 cvdriscpoisson.csv")
 


##htn aware 3 cvdrisc

htn_aware <- glm.cluster(formula = htn_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_diab_broad_ind + d_id ,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(htn_aware), confint(htn_aware)))
write.csv(aware, "RR htn aware 3 cvdrisc poisson.csv")

results_aware <-summary(htn_aware)
write.csv(results_aware, "p Value htn aware 3 cvdriscpoisson.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn aware 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn aware 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn aware 3 cvdriscpoisson.csv")



##htn treated 3 cvdrisc

htn_treated <- glm.cluster(formula = htn_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(htn_treated), confint(htn_treated)))
write.csv(treated, "RR htn treated 3 cvdrisc poisson.csv")

results_treated <-summary(htn_treated)
write.csv(results_treated, "p Value htn treated 3 cvdriscpoisson.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn treated 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn treated 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn treated 3 cvdriscpoisson.csv")


##htn controlled 3 cvdrisc

htn_controlled <- glm.cluster(formula = htn_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(htn_controlled), confint(htn_controlled)))
write.csv(controlled, "RR htn controlled 3 cvdrisc poisson.csv")

results_controlled <-summary(htn_controlled)
write.csv(results_controlled, "p Value htn controlled 3 cvdriscpoisson.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn controlled 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn controlled 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn controlled 3 cvdriscpoisson.csv")


```




```{r multivariable regression combined cascade with obese and smoke}

##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)


##Diabetes aware

diabetes_aware <- glm.cluster(formula = both_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))


aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
aware <- as.data.frame(aware, optional = TRUE)

write.csv(aware, "OR diabetes aware .csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes aware.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes aware .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases aware with obese and smoking.csv")
 



##Diabetes treated

diabetes_treated <- glm.cluster(formula = both_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "OR diabetes treated .csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treated.csv")



######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes treated .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases treated with obese and smoking.csv")
 


##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = both_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "OR diabetes controlled .csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlled.csv")



######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes controlled .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlled.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases controlled with obese and smoking.csv")
 

```


```{r multivariable regressions diabetes cascade with all 5 comorbidities}


##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1)


##diabetes aware 3 cvdrisc

diabetes_aware <- glm.cluster(formula = diabetic_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_htn_broad_ind + asthma + anemia + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
write.csv(aware, "RR diabetes aware 3 cvdrisc poisson  all 5 comorbidities.csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes aware 3 cvdriscpoisson  all 5 comorbidities.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes aware 3 cvdrisc poisson  all 5 comorbidities.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware 3 cvdriscpoisson  all 5 comorbidities.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes aware 3 cvdrisc single disease diabetes aware 3 cvdrisc all 5 comorbidities.csv")
 


##diabetes treated 3 cvdrisc

diabetes_treated <- glm.cluster(formula = diabetic_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_htn_broad_ind + asthma + anemia + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "RR diabetes treated 3 cvdrisc poisson  all 5 comorbidities.csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treated 3 cvdriscpoisson  all 5 comorbidities.csv")

######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes treated 3 cvdrisc poisson  all 5 comorbidities.csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated 3 cvdriscpoisson  all 5 comorbidities.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes treated 3 cvdrisc single disease diabetes treated 3 cvdrisc all 5 comorbidities.csv")
 



##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = diabetic_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + ex_htn_broad_ind + asthma + anemia + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "RR diabetes controlled 3 cvdriscpoisson  all 5 comorbidities.csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlledpoisson  all 5 comorbidities.csv")

######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes controlled 3 cvdriscpoisson  all 5 comorbidities.csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlledpoisson  all 5 comorbidities.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes controlled"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes controlled 3 cvdriscsingle disease diabetes controlled all 5 comorbidities.csv")
 




```


```{r multivariable regressions htn cascade with all 5 comorbidities}


##filter
dhs_nomiss_htn_only <- filter(dhs_nomiss, ex_htn_broad_ind==1) 

##htn screened 3 cvdrisc

htn_screened <- glm.cluster(formula = htn_screened_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex +  obese + tobacco_smoked + ex_diab_broad_ind + asthma + anemia + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

screened <- exp(cbind(RR = coef(htn_screened), confint(htn_screened)))
write.csv(screened, "RR htn screened 3 cvdrisc poisson all 5 comorbidities.csv")

results_screened <-summary(htn_screened)
write.csv(results_screened, "p Value htn screened 3 cvdriscpoisson all 5 comorbidities.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn screened 3 cvdrisc poisson all 5 comorbidities.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn screened 3 cvdriscpoisson all 5 comorbidities.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn screened 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn screened 3 cvdriscpoisson all 5 comorbidities.csv")
 


##htn aware 3 cvdrisc

htn_aware <- glm.cluster(formula = htn_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_diab_broad_ind + asthma + anemia + d_id ,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(htn_aware), confint(htn_aware)))
write.csv(aware, "RR htn aware 3 cvdrisc poisson all 5 comorbidities.csv")

results_aware <-summary(htn_aware)
write.csv(results_aware, "p Value htn aware 3 cvdriscpoisson all 5 comorbidities.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn aware 3 cvdrisc poisson all 5 comorbidities.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn aware 3 cvdriscpoisson all 5 comorbidities.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn aware 3 cvdriscpoisson all 5 comorbidities.csv")



##htn treated 3 cvdrisc

htn_treated <- glm.cluster(formula = htn_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_diab_broad_ind + asthma + anemia + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(htn_treated), confint(htn_treated)))
write.csv(treated, "RR htn treated 3 cvdrisc poisson all 5 comorbidities.csv")

results_treated <-summary(htn_treated)
write.csv(results_treated, "p Value htn treated 3 cvdriscpoisson all 5 comorbidities.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn treated 3 cvdrisc poisson all 5 comorbidities.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn treated 3 cvdriscpoisson all 5 comorbidities.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn treated 3 cvdriscpoisson all 5 comorbidities.csv")


##htn controlled 3 cvdrisc

htn_controlled <- glm.cluster(formula = htn_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + ex_diab_broad_ind + asthma + anemia + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(htn_controlled), confint(htn_controlled)))
write.csv(controlled, "RR htn controlled 3 cvdrisc poisson all 5 comorbidities.csv")

results_controlled <-summary(htn_controlled)
write.csv(results_controlled, "p Value htn controlled 3 cvdriscpoisson all 5 comorbidities.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn controlled 3 cvdrisc poisson all 5 comorbidities.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn controlled 3 cvdriscpoisson all 5 comorbidities.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn controlled 3 cvdriscpoisson all 5 comorbidities.csv")


```




```{r multivariable regression combined cascade with all 5 comorbidities}

##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)


##Diabetes aware

diabetes_aware <- glm.cluster(formula = both_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + asthma + anemia + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))


aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
aware <- as.data.frame(aware, optional = TRUE)

write.csv(aware, "OR diabetes aware  all 5 comorbidities.csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes aware all 5 comorbidities.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes aware  all 5 comorbidities.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware all 5 comorbidities.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases aware with obese and smoking all 5 comorbidities.csv")
 



##Diabetes treated

diabetes_treated <- glm.cluster(formula = both_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + asthma + anemia  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "OR diabetes treated  all 5 comorbidities.csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treated all 5 comorbidities.csv")



######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes treated  all 5 comorbidities.csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated all 5 comorbidities.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases treated with obese and smoking all 5 comorbidities.csv")
 


##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = both_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + asthma + anemia + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "OR diabetes controlled  all 5 comorbidities.csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlled all 5 comorbidities.csv")



######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes controlled  all 5 comorbidities.csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlled all 5 comorbidities.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases controlled with obese and smoking all 5 comorbidities.csv")
 

```





```{r multivariable regression combined cascade with obese and smoke women only}

##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1 & sex==1)


##Diabetes aware

diabetes_aware <- glm.cluster(formula = both_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))


aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
aware <- as.data.frame(aware, optional = TRUE)

write.csv(aware, "OR diabetes aware .csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes aware.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes aware .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases aware with obese and smoking women only.csv")
 



##Diabetes treated

diabetes_treated <- glm.cluster(formula = both_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "OR diabetes treated .csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treated.csv")



######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes treated .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases treated with obese and smoking women only.csv")
 


##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = both_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "OR diabetes controlled .csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlled.csv")



######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes controlled .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlled.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases controlled with obese and smoking women only.csv")
 

```


```{r multivariable regressions diabetes cascade with obese and smoke and htn women only}


##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & sex==1)


##diabetes aware 3 cvdrisc

diabetes_aware <- glm.cluster(formula = diabetic_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_htn_broad_ind + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
write.csv(aware, "RR diabetes aware 3 cvdrisc poisson .csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes aware 3 cvdriscpoisson .csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes aware 3 cvdrisc poisson .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware 3 cvdriscpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes aware women only.csv")
 


##diabetes treated 3 cvdrisc

diabetes_treated <- glm.cluster(formula = diabetic_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_htn_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "RR diabetes treated 3 cvdrisc poisson .csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treated 3 cvdriscpoisson .csv")

######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes treated 3 cvdrisc poisson .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated 3 cvdriscpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes treated women only.csv")
 



##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = diabetic_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + ex_htn_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "RR diabetes controlled 3 cvdriscpoisson .csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlledpoisson .csv")

######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes controlled 3 cvdriscpoisson .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlledpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes controlled"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes controlled women only.csv")
 




```


```{r multivariable regressions htn cascade with obese and smoke and diabetes women only}


##filter
dhs_nomiss_htn_only <- filter(dhs_nomiss, ex_htn_broad_ind==1 & sex==1) 

##htn screened 3 cvdrisc

htn_screened <- glm.cluster(formula = htn_screened_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex +  obese + tobacco_smoked + ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

screened <- exp(cbind(RR = coef(htn_screened), confint(htn_screened)))
write.csv(screened, "RR htn screened 3 cvdrisc poisson.csv")

results_screened <-summary(htn_screened)
write.csv(results_screened, "p Value htn screened 3 cvdriscpoisson.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn screened 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn screened 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn screened 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn screened women only.csv")
 


##htn aware 3 cvdrisc

htn_aware <- glm.cluster(formula = htn_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_diab_broad_ind + d_id ,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(htn_aware), confint(htn_aware)))
write.csv(aware, "RR htn aware 3 cvdrisc poisson.csv")

results_aware <-summary(htn_aware)
write.csv(results_aware, "p Value htn aware 3 cvdriscpoisson.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn aware 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn aware 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn aware women only.csv")



##htn treated 3 cvdrisc

htn_treated <- glm.cluster(formula = htn_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(htn_treated), confint(htn_treated)))
write.csv(treated, "RR htn treated 3 cvdrisc poisson.csv")

results_treated <-summary(htn_treated)
write.csv(results_treated, "p Value htn treated 3 cvdriscpoisson.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn treated 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn treated 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn treated women only.csv")


##htn controlled 3 cvdrisc

htn_controlled <- glm.cluster(formula = htn_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(htn_controlled), confint(htn_controlled)))
write.csv(controlled, "RR htn controlled 3 cvdrisc poisson.csv")

results_controlled <-summary(htn_controlled)
write.csv(results_controlled, "p Value htn controlled 3 cvdriscpoisson.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn controlled 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn controlled 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn controlled women only.csv")


```





```{r multivariable regression combined cascade with obese and smoke men only}

##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1 & sex==0)


##Diabetes aware

diabetes_aware <- glm.cluster(formula = both_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))


aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
aware <- as.data.frame(aware, optional = TRUE)

write.csv(aware, "OR diabetes aware .csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes aware.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes aware .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases aware with obese and smoking men only.csv")
 



##Diabetes treated

diabetes_treated <- glm.cluster(formula = both_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "OR diabetes treated .csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treated.csv")



######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes treated .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases treated with obese and smoking men only.csv")
 


##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = both_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "OR diabetes controlled .csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlled.csv")



######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes controlled .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlled.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases controlled with obese and smoking men only.csv")
 

```


```{r multivariable regressions diabetes cascade with obese and smoke and htn men only}


##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & sex==0)


##diabetes aware 3 cvdrisc

diabetes_aware <- glm.cluster(formula = diabetic_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_htn_broad_ind + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
write.csv(aware, "RR diabetes aware 3 cvdrisc poisson .csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes aware 3 cvdriscpoisson .csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes aware 3 cvdrisc poisson .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware 3 cvdriscpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes aware men only.csv")
 


##diabetes treated 3 cvdrisc

diabetes_treated <- glm.cluster(formula = diabetic_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_htn_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "RR diabetes treated 3 cvdrisc poisson .csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treated 3 cvdriscpoisson .csv")

######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes treated 3 cvdrisc poisson .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated 3 cvdriscpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes treated men only.csv")
 



##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = diabetic_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + ex_htn_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "RR diabetes controlled 3 cvdriscpoisson .csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlledpoisson .csv")

######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes controlled 3 cvdriscpoisson .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlledpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes controlled"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes controlled men only.csv")
 




```


```{r multivariable regressions htn cascade with obese and smoke and diabetes men only}


##filter
dhs_nomiss_htn_only <- filter(dhs_nomiss, ex_htn_broad_ind==1 & sex==0) 

##htn screened 3 cvdrisc

htn_screened <- glm.cluster(formula = htn_screened_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex +  obese + tobacco_smoked + ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

screened <- exp(cbind(RR = coef(htn_screened), confint(htn_screened)))
write.csv(screened, "RR htn screened 3 cvdrisc poisson.csv")

results_screened <-summary(htn_screened)
write.csv(results_screened, "p Value htn screened 3 cvdriscpoisson.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn screened 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn screened 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn screened 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn screened men only.csv")
 


##htn aware 3 cvdrisc

htn_aware <- glm.cluster(formula = htn_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_diab_broad_ind + d_id ,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(htn_aware), confint(htn_aware)))
write.csv(aware, "RR htn aware 3 cvdrisc poisson.csv")

results_aware <-summary(htn_aware)
write.csv(results_aware, "p Value htn aware 3 cvdriscpoisson.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn aware 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn aware 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn aware men only.csv")



##htn treated 3 cvdrisc

htn_treated <- glm.cluster(formula = htn_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(htn_treated), confint(htn_treated)))
write.csv(treated, "RR htn treated 3 cvdrisc poisson.csv")

results_treated <-summary(htn_treated)
write.csv(results_treated, "p Value htn treated 3 cvdriscpoisson.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn treated 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn treated 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn treated men only.csv")


##htn controlled 3 cvdrisc

htn_controlled <- glm.cluster(formula = htn_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(htn_controlled), confint(htn_controlled)))
write.csv(controlled, "RR htn controlled 3 cvdrisc poisson.csv")

results_controlled <-summary(htn_controlled)
write.csv(results_controlled, "p Value htn controlled 3 cvdriscpoisson.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn controlled 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn controlled 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn controlled men only.csv")


```






```{r multivariable regression combined cascade sociodemographics only women only}

##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1 & sex==1)


## aware

diabetes_aware <- glm.cluster(formula = both_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))


aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
aware <- as.data.frame(aware, optional = TRUE)

write.csv(aware, "OR diabetes aware .csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes aware.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes aware .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases aware sociodemographics only women only.csv")
 



## treated

diabetes_treated <- glm.cluster(formula = both_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "OR diabetes treated .csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treated.csv")



######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes treated .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases treated sociodemographics only women only.csv")
 


##controlled

diabetes_controlled <- glm.cluster(formula = both_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "OR diabetes controlled .csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlled.csv")



######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes controlled .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlled.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases controlled sociodemographics only women only.csv")
 

```




```{r multivariable regression combined cascade sociodemographics only men only}

##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1 & sex==0)

 dhs_nomiss_diabetic_only <- dplyr::mutate(dhs_nomiss_diabetic_only, age_grp_superlarge = ifelse(age<=28, "15-28", 
                                           ifelse(age>28 & age<=41, "29-41","42-54")))
dhs_nomiss_diabetic_only$age_grp_superlarge <- factor(dhs_nomiss_diabetic_only$age_grp_superlarge, levels = c("15-28", "29-41", "42-54"))
dhs_nomiss_diabetic_only <- within(dhs_nomiss_diabetic_only, age_grp_superlarge <- relevel(age_grp_superlarge, ref = "15-28"))

## aware

diabetes_aware <- glm.cluster(formula = both_aware_dbl ~ age_grp_superlarge + wealth_quintile_rurb + educatnames + married + urban+ d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))


aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
aware <- as.data.frame(aware, optional = TRUE)
write.csv(aware, "OR diabetes aware .csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes aware.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes aware .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases aware sociodemographics only men only.csv")
 



## treated

diabetes_treated <- glm.cluster(formula = both_treated_dbl ~ age_grp_superlarge + wealth_quintile_rurb + educatnames + married + urban  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
treated <- as.data.frame(treated, optional = TRUE)
write.csv(treated, "OR diabetes treated .csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treated.csv")



######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes treated .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases treated sociodemographics only men only.csv")
 


 
 

##controlled

diabetes_controlled <- glm.cluster(formula = both_controlled_dbl ~ age_grp_superlarge + wealth_quintile_rurb + educatnames + married + urban + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "OR diabetes controlled .csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlled.csv")



######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes controlled .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlled.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases controlled sociodemographics only men only.csv")
 

```




```{r multivariable regressions diabetes cascade with obese and smoke and htn}


##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1)


##diabetes aware 3 cvdrisc

diabetes_aware <- glm.cluster(formula = diabetic_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_htn_broad_ind + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
write.csv(aware, "RR diabetes aware 3 cvdrisc poisson .csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes aware 3 cvdriscpoisson .csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes aware 3 cvdrisc poisson .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware 3 cvdriscpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes aware 3 cvdrisc single disease diabetes aware 3 cvdrisc.csv")
 


##diabetes treated 3 cvdrisc

diabetes_treated <- glm.cluster(formula = diabetic_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_htn_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "RR diabetes treated 3 cvdrisc poisson .csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treated 3 cvdriscpoisson .csv")

######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes treated 3 cvdrisc poisson .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated 3 cvdriscpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes treated 3 cvdrisc single disease diabetes treated 3 cvdrisc.csv")
 



##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = diabetic_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + ex_htn_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "RR diabetes controlled 3 cvdriscpoisson .csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlledpoisson .csv")

######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes controlled 3 cvdriscpoisson .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlledpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes controlled"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes controlled 3 cvdriscsingle disease diabetes controlled.csv")
 




```


```{r multivariable regressions htn cascade with obese and smoke and diabetes}


##filter
dhs_nomiss_htn_only <- filter(dhs_nomiss, ex_htn_broad_ind==1) 

##htn screened 3 cvdrisc

htn_screened <- glm.cluster(formula = htn_screened_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex +  obese + tobacco_smoked + ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

screened <- exp(cbind(RR = coef(htn_screened), confint(htn_screened)))
write.csv(screened, "RR htn screened 3 cvdrisc poisson.csv")

results_screened <-summary(htn_screened)
write.csv(results_screened, "p Value htn screened 3 cvdriscpoisson.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn screened 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn screened 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn screened 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn screened 3 cvdriscpoisson.csv")
 


##htn aware 3 cvdrisc

htn_aware <- glm.cluster(formula = htn_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_diab_broad_ind + d_id ,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(htn_aware), confint(htn_aware)))
write.csv(aware, "RR htn aware 3 cvdrisc poisson.csv")

results_aware <-summary(htn_aware)
write.csv(results_aware, "p Value htn aware 3 cvdriscpoisson.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn aware 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn aware 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn aware 3 cvdriscpoisson.csv")



##htn treated 3 cvdrisc

htn_treated <- glm.cluster(formula = htn_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(htn_treated), confint(htn_treated)))
write.csv(treated, "RR htn treated 3 cvdrisc poisson.csv")

results_treated <-summary(htn_treated)
write.csv(results_treated, "p Value htn treated 3 cvdriscpoisson.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn treated 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn treated 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn treated 3 cvdriscpoisson.csv")


##htn controlled 3 cvdrisc

htn_controlled <- glm.cluster(formula = htn_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(htn_controlled), confint(htn_controlled)))
write.csv(controlled, "RR htn controlled 3 cvdrisc poisson.csv")

results_controlled <-summary(htn_controlled)
write.csv(results_controlled, "p Value htn controlled 3 cvdriscpoisson.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn controlled 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn controlled 3 cvdriscpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn controlled 3 cvdriscpoisson.csv")


```









```{r multivariable regression combined cascade with obese and smoke with weights}

##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)


##Diabetes aware

diabetes_aware <- glm(formula = both_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + d_id ,    weights= p_wt_new,  data=dhs_nomiss_diabetic_only, family=poisson(link="log"))


aware <- exp(cbind(RR = coef(diabetes_aware), confint.default(diabetes_aware)))
aware <- as.data.frame(aware, optional = TRUE)

write.csv(aware, "OR diabetes aware .csv")

results_aware <-summary(diabetes_aware)$coefficients
write.csv(results_aware, "p Value diabetes aware.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes aware .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases aware with obese and smoking with weights.csv")
 



##Diabetes treated

diabetes_treated <- glm(formula = both_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked  + d_id,    weights= p_wt_new,  data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint.default(diabetes_treated)))
write.csv(treated, "OR diabetes treated .csv")

results_treated <-summary(diabetes_treated)$coefficients
write.csv(results_treated, "p Value diabetes treated.csv")



######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes treated .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases treated with obese and smoking with weights.csv")
 


##Diabetes controlled

diabetes_controlled <- glm(formula = both_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked  + d_id,    weights= p_wt_new,  data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint.default(diabetes_controlled)))
write.csv(controlled, "OR diabetes controlled .csv")

results_controlled <-summary(diabetes_controlled)$coefficients
write.csv(results_controlled, "p Value diabetes controlled  with weights.csv")



######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes controlled .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlled  with weights.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases controlled with obese and smoking with weights.csv")
 

```


```{r multivariable regressions diabetes cascade with obese and smoke and htn with weights}


##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1)


##diabetes aware 3 cvdrisc

diabetes_aware <- glm(formula = diabetic_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_htn_broad_ind + d_id ,    weights= p_wt_new,  data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(diabetes_aware), confint.default(diabetes_aware)))
write.csv(aware, "RR diabetes aware 3 cvdrisc poisson .csv")

results_aware <-summary(diabetes_aware)$coefficients
write.csv(results_aware, "p Value diabetes aware 3 cvdriscpoisson .csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes aware 3 cvdrisc poisson .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware 3 cvdriscpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes aware 3 cvdrisc single disease diabetes aware 3 cvdrisc with weights.csv")
 


##diabetes treated 3 cvdrisc

diabetes_treated <- glm(formula = diabetic_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_htn_broad_ind + d_id,    weights= p_wt_new,  data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint.default(diabetes_treated)))
write.csv(treated, "RR diabetes treated 3 cvdrisc poisson .csv")

results_treated <-summary(diabetes_treated)$coefficients
write.csv(results_treated, "p Value diabetes treated 3 cvdriscpoisson .csv")

######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes treated 3 cvdrisc poisson .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated 3 cvdriscpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes treated 3 cvdrisc single disease diabetes treated 3 cvdrisc with weights.csv")
 



##Diabetes controlled

diabetes_controlled <- glm(formula = diabetic_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + ex_htn_broad_ind + d_id,    weights= p_wt_new,  data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint.default(diabetes_controlled)))
write.csv(controlled, "RR diabetes controlled 3 cvdriscpoisson .csv")

results_controlled <-summary(diabetes_controlled)$coefficients
write.csv(results_controlled, "p Value diabetes controlledpoisson .csv")

######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes controlled 3 cvdriscpoisson .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlledpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes controlled"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes controlled 3 cvdriscsingle disease diabetes controlled  with weights.csv")
 




```


```{r multivariable regressions htn cascade with obese and smoke and diabetes with weights}


##filter
dhs_nomiss_htn_only <- filter(dhs_nomiss, ex_htn_broad_ind==1) 

##htn screened 3 cvdrisc

htn_screened <- glm(formula = htn_screened_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex +  obese + tobacco_smoked + ex_diab_broad_ind + d_id,    weights= p_wt_new,  data=dhs_nomiss_htn_only, family=poisson(link="log"))

screened <- exp(cbind(RR = coef(htn_screened), confint.default(htn_screened)))
write.csv(screened, "RR htn screened 3 cvdrisc poisson.csv")

results_screened <-summary(htn_screened)$coefficients
write.csv(results_screened, "p Value htn screened 3 cvdriscpoisson with weights.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn screened 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn screened 3 cvdriscpoisson with weights.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn screened 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn screened 3 cvdriscpoisson with weights.csv")
 


##htn aware 3 cvdrisc

htn_aware <- glm(formula = htn_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_diab_broad_ind + d_id ,    weights= p_wt_new,  data=dhs_nomiss_htn_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(htn_aware), confint.default(htn_aware)))
write.csv(aware, "RR htn aware 3 cvdrisc poisson.csv")

results_aware <-summary(htn_aware)$coefficients
write.csv(results_aware, "p Value htn aware 3 cvdriscpoisson with weights.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn aware 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn aware 3 cvdriscpoisson with weights.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn aware 3 cvdriscpoisson with weights.csv")



##htn treated 3 cvdrisc

htn_treated <- glm(formula = htn_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + obese + tobacco_smoked + ex_diab_broad_ind + d_id,    weights= p_wt_new,  data=dhs_nomiss_htn_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(htn_treated), confint.default(htn_treated)))
write.csv(treated, "RR htn treated 3 cvdrisc poisson.csv")

results_treated <-summary(htn_treated)$coefficients
write.csv(results_treated, "p Value htn treated 3 cvdriscpoisson with weights.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn treated 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn treated 3 cvdriscpoisson with weights.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn treated 3 cvdriscpoisson with weights.csv")


##htn controlled 3 cvdrisc

htn_controlled <- glm(formula = htn_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + obese + tobacco_smoked + ex_diab_broad_ind + d_id,    weights= p_wt_new,  data=dhs_nomiss_htn_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(htn_controlled), confint.default(htn_controlled)))
write.csv(controlled, "RR htn controlled 3 cvdrisc poisson.csv")

results_controlled <-summary(htn_controlled)$coefficients
write.csv(results_controlled, "p Value htn controlled 3 cvdriscpoisson with weights.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn controlled 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn controlled 3 cvdriscpoisson with weights.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn controlled 3 cvdriscpoisson with weights.csv")


```





```{r multivariable regression combined cascade only sociodemographics with weights}

##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)


##Diabetes aware

diabetes_aware <- glm(formula = both_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + d_id ,    weights= p_wt_new,  data=dhs_nomiss_diabetic_only, family=poisson(link="log"))


aware <- exp(cbind(RR = coef(diabetes_aware), confint.default(diabetes_aware)))
aware <- as.data.frame(aware, optional = TRUE)

write.csv(aware, "OR diabetes aware .csv")

results_aware <-summary(diabetes_aware)$coefficients
write.csv(results_aware, "p Value diabetes aware.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes aware .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases aware sociodemographics with weights.csv")
 



##Diabetes treated

diabetes_treated <- glm(formula = both_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + d_id,    weights= p_wt_new,  data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint.default(diabetes_treated)))
write.csv(treated, "OR diabetes treated .csv")

results_treated <-summary(diabetes_treated)$coefficients
write.csv(results_treated, "p Value diabetes treated.csv")



######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes treated .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases treated sociodemographics with weights.csv")
 


##Diabetes controlled

diabetes_controlled <- glm(formula = both_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex   + d_id,    weights= p_wt_new,  data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint.default(diabetes_controlled)))
write.csv(controlled, "OR diabetes controlled .csv")

results_controlled <-summary(diabetes_controlled)$coefficients
write.csv(results_controlled, "p Value diabetes controlled  with weights.csv")



######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/OR diabetes controlled .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlled  with weights.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "both diseases controlled sociodemographics with weights.csv")
 

```


```{r multivariable regressions diabetes cascade only sociodemographics  with weights}


##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1)


##diabetes aware 3 cvdrisc

diabetes_aware <- glm(formula = diabetic_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex   + d_id ,    weights= p_wt_new,  data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(diabetes_aware), confint.default(diabetes_aware)))
write.csv(aware, "RR diabetes aware 3 cvdrisc poisson .csv")

results_aware <-summary(diabetes_aware)$coefficients
write.csv(results_aware, "p Value diabetes aware 3 sociodemographics .csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes aware 3 cvdrisc poisson .csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes aware 3 sociodemographics .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes aware 3 cvdrisc sociodemographics diabetes aware 3 cvdrisc with weights.csv")
 


##diabetes treated 3 cvdrisc

diabetes_treated <- glm(formula = diabetic_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + d_id,    weights= p_wt_new,  data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint.default(diabetes_treated)))
write.csv(treated, "RR diabetes treated 3 cvdrisc poisson .csv")

results_treated <-summary(diabetes_treated)$coefficients
write.csv(results_treated, "p Value diabetes treated 3 sociodemographics .csv")

######FORMATTING


treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes treated 3 cvdrisc poisson .csv")



colnames(treated)[colnames(treated)=="X2.5.."] <- "lowci"
colnames(treated)[colnames(treated)=="X97.5.."] <- "uppci"

 treated <- mutate(treated,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

treated$lowci <- sprintf("%.2f", treated$lowci)
treated$RR <- sprintf("%.2f", treated$RR)
treated$uppci <- sprintf("%.2f", treated$uppci)



  treated <- mutate(treated,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  treated <- treated %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treated 3 sociodemographics .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(treated, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "diabetes treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes treated 3 cvdrisc sociodemographics diabetes treated 3 cvdrisc with weights.csv")
 



##Diabetes controlled

diabetes_controlled <- glm(formula = diabetic_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + d_id,    weights= p_wt_new,  data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint.default(diabetes_controlled)))
write.csv(controlled, "RR diabetes controlled 3 sociodemographics .csv")

results_controlled <-summary(diabetes_controlled)$coefficients
write.csv(results_controlled, "p Value diabetes controlledpoisson .csv")

######FORMATTING


controlled <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes controlled 3 sociodemographics .csv")



colnames(controlled)[colnames(controlled)=="X2.5.."] <- "lowci"
colnames(controlled)[colnames(controlled)=="X97.5.."] <- "uppci"

 controlled <- mutate(controlled,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

controlled$lowci <- sprintf("%.2f", controlled$lowci)
controlled$RR <- sprintf("%.2f", controlled$RR)
controlled$uppci <- sprintf("%.2f", controlled$uppci)



  controlled <- mutate(controlled,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  controlled <- controlled %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlledpoisson .csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(controlled, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes controlled"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes controlled 3 cvdriscsociodemographics diabetes controlled  with weights.csv")
 




```


```{r multivariable regressions htn cascade only sociodemographics with weights}


##filter
dhs_nomiss_htn_only <- filter(dhs_nomiss, ex_htn_broad_ind==1) 

##htn screened 3 cvdrisc

htn_screened <- glm(formula = htn_screened_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + d_id,    weights= p_wt_new,  data=dhs_nomiss_htn_only, family=poisson(link="log"))

screened <- exp(cbind(RR = coef(htn_screened), confint.default(htn_screened)))
write.csv(screened, "RR htn screened 3 cvdrisc poisson.csv")

results_screened <-summary(htn_screened)$coefficients
write.csv(results_screened, "p Value htn screened 3 sociodemographics with weights.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn screened 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn screened 3 sociodemographics with weights.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn screened 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn screened 3 sociodemographics with weights.csv")
 


##htn aware 3 cvdrisc

htn_aware <- glm(formula = htn_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + d_id ,    weights= p_wt_new,  data=dhs_nomiss_htn_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(htn_aware), confint.default(htn_aware)))
write.csv(aware, "RR htn aware 3 cvdrisc poisson.csv")

results_aware <-summary(htn_aware)$coefficients
write.csv(results_aware, "p Value htn aware 3 sociodemographics with weights.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn aware 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn aware 3 sociodemographics with weights.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn aware 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn aware 3 sociodemographics with weights.csv")



##htn treated 3 cvdrisc

htn_treated <- glm(formula = htn_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + d_id,    weights= p_wt_new,  data=dhs_nomiss_htn_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(htn_treated), confint.default(htn_treated)))
write.csv(treated, "RR htn treated 3 cvdrisc poisson.csv")

results_treated <-summary(htn_treated)$coefficients
write.csv(results_treated, "p Value htn treated 3 sociodemographics with weights.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn treated 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn treated 3 sociodemographics with weights.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn treated 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn treated 3 sociodemographics with weights.csv")


##htn controlled 3 cvdrisc

htn_controlled <- glm(formula = htn_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex  + d_id,    weights= p_wt_new,  data=dhs_nomiss_htn_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(htn_controlled), confint.default(htn_controlled)))
write.csv(controlled, "RR htn controlled 3 cvdrisc poisson.csv")

results_controlled <-summary(htn_controlled)$coefficients
write.csv(results_controlled, "p Value htn controlled 3 sociodemographics with weights.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR htn controlled 3 cvdrisc poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value htn controlled 3 sociodemographics with weights.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "htn controlled 3 cvdrisc"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL htn controlled 3 sociodemographics with weights.csv")


```







```{r univariable regressions combined cascade}

dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)


##AWARE

age <- glm.cluster(formula = both_aware_dbl ~ age_grpOR + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results combined cascade univariable reg aware.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value combined cascade univariable reg aware.csv")

wealth <- glm.cluster(formula = both_aware_dbl ~  wealth_quintile_rurb + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results combined cascade  univariable reg.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value combined cascade univariable reg.csv")

educat <- glm.cluster(formula = both_aware_dbl ~  educatnames + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results combined cascade univariable reg aware.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value combined cascade univariable reg aware.csv")

married <- glm.cluster(formula = both_aware_dbl ~  married + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results combined cascade univariable reg aware.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value combined cascade univariable reg aware.csv")

urban <- glm.cluster(formula = both_aware_dbl ~ urban + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
urbansheet <- exp(cbind(RR = coef(urban), confint(urban)))
write.csv(urbansheet, "urban RR regression results combined cascade univariable reg aware.csv")

urban_reg <-summary(urban)
write.csv(urban_reg, "urban p-Value combined cascade univariable reg aware.csv")

sex <- glm.cluster(formula = both_aware_dbl ~  sex + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results combined cascade univariable reg aware.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value combined cascade univariable reg aware.csv")

obese <- glm.cluster(formula = both_aware_dbl ~ obese + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
obesesheet <- exp(cbind(RR = coef(obese), confint(obese)))
write.csv(obesesheet, "obese RR regression results combined cascade univariable reg aware.csv")

obese_reg <-summary(obese)
write.csv(obese_reg, "obese p-Value combined cascade univariable reg aware.csv")

tobacco_smoked <- glm.cluster(formula = both_aware_dbl ~ tobacco_smoked + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
tobacco_smokedsheet <- exp(cbind(RR = coef(tobacco_smoked), confint(tobacco_smoked)))
write.csv(tobacco_smokedsheet, "tobacco_smoked RR regression results combined cascade univariable reg aware.csv")

tobacco_smoked_reg <-summary(tobacco_smoked)
write.csv(tobacco_smoked_reg, "tobacco_smoked p-Value combined cascade univariable reg aware.csv")


##Treated


age <- glm.cluster(formula = both_treated_dbl ~ age_grpOR + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results combined cascade univariable reg treated.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value combined cascade univariable reg treated.csv")

wealth <- glm.cluster(formula = both_treated_dbl ~  wealth_quintile_rurb + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results combined cascade  univariable reg treated.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value combined cascade univariable reg treated.csv")

educat <- glm.cluster(formula = both_treated_dbl ~  educatnames + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results combined cascade univariable reg treated.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value combined cascade univariable reg treated.csv")

married <- glm.cluster(formula = both_treated_dbl ~  married + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results combined cascade univariable reg treated.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value combined cascade univariable reg treated.csv")

urban <- glm.cluster(formula = both_treated_dbl ~ urban + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
urbansheet <- exp(cbind(RR = coef(urban), confint(urban)))
write.csv(urbansheet, "urban RR regression results combined cascade univariable reg treated.csv")

urban_reg <-summary(urban)
write.csv(urban_reg, "urban p-Value combined cascade univariable reg treated.csv")

sex <- glm.cluster(formula = both_treated_dbl ~  sex + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results combined cascade univariable reg treated.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value combined cascade univariable reg treated.csv")

obese <- glm.cluster(formula = both_treated_dbl ~ obese + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
obesesheet <- exp(cbind(RR = coef(obese), confint(obese)))
write.csv(obesesheet, "obese RR regression results combined cascade univariable reg treated.csv")

obese_reg <-summary(obese)
write.csv(obese_reg, "obese p-Value combined cascade univariable reg treated.csv")

tobacco_smoked <- glm.cluster(formula = both_treated_dbl ~ tobacco_smoked + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
tobacco_smokedsheet <- exp(cbind(RR = coef(tobacco_smoked), confint(tobacco_smoked)))
write.csv(tobacco_smokedsheet, "tobacco_smoked RR regression results combined cascade univariable reg treated.csv")

tobacco_smoked_reg <-summary(tobacco_smoked)
write.csv(tobacco_smoked_reg, "tobacco_smoked p-Value combined cascade univariable reg treated.csv")

####Both controlled



age <- glm.cluster(formula = both_controlled_dbl ~ age_grpOR + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
agesheet <- exp(cbind(RR = coef(age), confint(age)))
write.csv(agesheet, "age RR regression results combined cascade univariable reg controlled.csv")

age_reg <-summary(age)
write.csv(age_reg, "age p-Value combined cascade univariable reg controlled.csv")

wealth <- glm.cluster(formula = both_controlled_dbl ~  wealth_quintile_rurb + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
wealthsheet <- exp(cbind(RR = coef(wealth), confint(wealth)))
write.csv(wealthsheet, "wealth RR regression results combined cascade  univariable reg controlled.csv")

wealth_reg <-summary(wealth)
write.csv(wealth_reg, "wealth p-Value combined cascade univariable reg controlled.csv")

educat <- glm.cluster(formula = both_controlled_dbl ~  educatnames + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
educatsheet <- exp(cbind(RR = coef(educat), confint(educat)))
write.csv(educatsheet, "educat RR regression results combined cascade univariable reg controlled.csv")

educat_reg <-summary(educat)
write.csv(educat_reg, "educat p-Value combined cascade univariable reg controlled.csv")

married <- glm.cluster(formula = both_controlled_dbl ~  married + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
marriedsheet <- exp(cbind(RR = coef(married), confint(married)))
write.csv(marriedsheet, "married RR regression results combined cascade univariable reg controlled.csv")

married_reg <-summary(married)
write.csv(married_reg, "married p-Value combined cascade univariable reg controlled.csv")

urban <- glm.cluster(formula = both_controlled_dbl ~ urban + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
urbansheet <- exp(cbind(RR = coef(urban), confint(urban)))
write.csv(urbansheet, "urban RR regression results combined cascade univariable reg controlled.csv")

urban_reg <-summary(urban)
write.csv(urban_reg, "urban p-Value combined cascade univariable reg controlled.csv")

sex <- glm.cluster(formula = both_controlled_dbl ~  sex + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
sexsheet <- exp(cbind(RR = coef(sex), confint(sex)))
write.csv(sexsheet, "sex RR regression results combined cascade univariable reg controlled.csv")

sex_reg <-summary(sex)
write.csv(sex_reg, "sex p-Value combined cascade univariable reg controlled.csv")

obese <- glm.cluster(formula = both_controlled_dbl ~ obese + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
obesesheet <- exp(cbind(RR = coef(obese), confint(obese)))
write.csv(obesesheet, "obese RR regression results combined cascade univariable reg controlled.csv")

obese_reg <-summary(obese)
write.csv(obese_reg, "obese p-Value combined cascade univariable reg controlled.csv")

tobacco_smoked <- glm.cluster(formula = both_controlled_dbl ~ tobacco_smoked + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
tobacco_smokedsheet <- exp(cbind(RR = coef(tobacco_smoked), confint(tobacco_smoked)))
write.csv(tobacco_smokedsheet, "tobacco_smoked RR regression results combined cascade univariable reg controlled.csv")

tobacco_smoked_reg <-summary(tobacco_smoked)
write.csv(tobacco_smoked_reg, "tobacco_smoked p-Value combined cascade univariable reg controlled.csv")




```



```{r univariable regressions hypertension cascade}

dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_htn_broad_ind==1)


##screened

obese <- glm.cluster(formula = htn_screened_dbl ~ obese + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
obesesheet <- exp(cbind(RR = coef(obese), confint(obese)))
write.csv(obesesheet, "obese RR regression results htn cascade univariable reg screened.csv")

obese_reg <-summary(obese)
write.csv(obese_reg, "obese p-Value htn cascade univariable reg screened.csv")

tobacco_smoked <- glm.cluster(formula = htn_screened_dbl ~ tobacco_smoked + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
tobacco_smokedsheet <- exp(cbind(RR = coef(tobacco_smoked), confint(tobacco_smoked)))
write.csv(tobacco_smokedsheet, "tobacco_smoked RR regression results htn cascade univariable reg screened.csv")

tobacco_smoked_reg <-summary(tobacco_smoked)
write.csv(tobacco_smoked_reg, "tobacco_smoked p-Value htn cascade univariable reg screened.csv")

ex_diab_broad_ind <- glm.cluster(formula = htn_screened_dbl ~ ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
ex_diab_broad_indsheet <- exp(cbind(RR = coef(ex_diab_broad_ind), confint(ex_diab_broad_ind)))
write.csv(ex_diab_broad_indsheet, "ex_diab_broad_ind RR regression results htn cascade univariable reg screened.csv")

ex_diab_broad_ind_reg <-summary(ex_diab_broad_ind)
write.csv(ex_diab_broad_ind_reg, "ex_diab_broad_ind p-Value htn cascade univariable reg screened.csv")



##AWARE

obese <- glm.cluster(formula = htn_aware_dbl ~ obese + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
obesesheet <- exp(cbind(RR = coef(obese), confint(obese)))
write.csv(obesesheet, "obese RR regression results htn cascade univariable reg aware.csv")

obese_reg <-summary(obese)
write.csv(obese_reg, "obese p-Value htn cascade univariable reg aware.csv")

tobacco_smoked <- glm.cluster(formula = htn_aware_dbl ~ tobacco_smoked + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
tobacco_smokedsheet <- exp(cbind(RR = coef(tobacco_smoked), confint(tobacco_smoked)))
write.csv(tobacco_smokedsheet, "tobacco_smoked RR regression results htn cascade univariable reg aware.csv")

tobacco_smoked_reg <-summary(tobacco_smoked)
write.csv(tobacco_smoked_reg, "tobacco_smoked p-Value htn cascade univariable reg aware.csv")

ex_diab_broad_ind <- glm.cluster(formula = htn_aware_dbl ~ ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
ex_diab_broad_indsheet <- exp(cbind(RR = coef(ex_diab_broad_ind), confint(ex_diab_broad_ind)))
write.csv(ex_diab_broad_indsheet, "ex_diab_broad_ind RR regression results htn cascade univariable reg aware.csv")

ex_diab_broad_ind_reg <-summary(ex_diab_broad_ind)
write.csv(ex_diab_broad_ind_reg, "ex_diab_broad_ind p-Value htn cascade univariable reg aware.csv")

##TREATED


obese <- glm.cluster(formula = htn_treated_dbl ~ obese + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
obesesheet <- exp(cbind(RR = coef(obese), confint(obese)))
write.csv(obesesheet, "obese RR regression results htn cascade univariable reg treated.csv")

obese_reg <-summary(obese)
write.csv(obese_reg, "obese p-Value htn cascade univariable reg treated.csv")

tobacco_smoked <- glm.cluster(formula = htn_treated_dbl ~ tobacco_smoked + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
tobacco_smokedsheet <- exp(cbind(RR = coef(tobacco_smoked), confint(tobacco_smoked)))
write.csv(tobacco_smokedsheet, "tobacco_smoked RR regression results htn cascade univariable reg treated.csv")

tobacco_smoked_reg <-summary(tobacco_smoked)
write.csv(tobacco_smoked_reg, "tobacco_smoked p-Value htn cascade univariable reg treated.csv")

ex_diab_broad_ind <- glm.cluster(formula = htn_treated_dbl ~ ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
ex_diab_broad_indsheet <- exp(cbind(RR = coef(ex_diab_broad_ind), confint(ex_diab_broad_ind)))
write.csv(ex_diab_broad_indsheet, "ex_diab_broad_ind RR regression results htn cascade univariable reg treated.csv")

ex_diab_broad_ind_reg <-summary(ex_diab_broad_ind)
write.csv(ex_diab_broad_ind_reg, "ex_diab_broad_ind p-Value htn cascade univariable reg treated.csv")

##CONTROLLED

obese <- glm.cluster(formula = htn_controlled_dbl ~ obese + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
obesesheet <- exp(cbind(RR = coef(obese), confint(obese)))
write.csv(obesesheet, "obese RR regression results htn cascade univariable reg controlled.csv")

obese_reg <-summary(obese)
write.csv(obese_reg, "obese p-Value htn cascade univariable reg controlled.csv")

tobacco_smoked <- glm.cluster(formula = htn_controlled_dbl ~ tobacco_smoked + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
tobacco_smokedsheet <- exp(cbind(RR = coef(tobacco_smoked), confint(tobacco_smoked)))
write.csv(tobacco_smokedsheet, "tobacco_smoked RR regression results htn cascade univariable reg controlled.csv")

tobacco_smoked_reg <-summary(tobacco_smoked)
write.csv(tobacco_smoked_reg, "tobacco_smoked p-Value htn cascade univariable reg controlled.csv")

ex_diab_broad_ind <- glm.cluster(formula = htn_controlled_dbl ~ ex_diab_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
ex_diab_broad_indsheet <- exp(cbind(RR = coef(ex_diab_broad_ind), confint(ex_diab_broad_ind)))
write.csv(ex_diab_broad_indsheet, "ex_diab_broad_ind RR regression results htn cascade univariable reg controlled.csv")

ex_diab_broad_ind_reg <-summary(ex_diab_broad_ind)
write.csv(ex_diab_broad_ind_reg, "ex_diab_broad_ind p-Value htn cascade univariable reg controlled.csv")






```



```{r univariable regressions diabetes cascade}

dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1)


###AWarE


obese <- glm.cluster(formula = diabetic_aware_dbl ~ obese + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
obesesheet <- exp(cbind(RR = coef(obese), confint(obese)))
write.csv(obesesheet, "obese RR regression results diabetic cascade univariable reg aware.csv")

obese_reg <-summary(obese)
write.csv(obese_reg, "obese p-Value diabetic cascade univariable reg aware.csv")

tobacco_smoked <- glm.cluster(formula = diabetic_aware_dbl ~ tobacco_smoked + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
tobacco_smokedsheet <- exp(cbind(RR = coef(tobacco_smoked), confint(tobacco_smoked)))
write.csv(tobacco_smokedsheet, "tobacco_smoked RR regression results diabetic cascade univariable reg aware.csv")

tobacco_smoked_reg <-summary(tobacco_smoked)
write.csv(tobacco_smoked_reg, "tobacco_smoked p-Value diabetic cascade univariable reg aware.csv")

ex_htn_broad_ind <- glm.cluster(formula = diabetic_aware_dbl ~ ex_htn_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
ex_htn_broad_indsheet <- exp(cbind(RR = coef(ex_htn_broad_ind), confint(ex_htn_broad_ind)))
write.csv(ex_htn_broad_indsheet, "ex_htn_broad_ind RR regression results htn cascade univariable reg aware.csv")

ex_htn_broad_ind_reg <-summary(ex_htn_broad_ind)
write.csv(ex_htn_broad_ind_reg, "ex_htn_broad_ind p-Value htn cascade univariable reg aware.csv")



### Treated 


obese <- glm.cluster(formula = diabetic_treated_dbl ~ obese + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
obesesheet <- exp(cbind(RR = coef(obese), confint(obese)))
write.csv(obesesheet, "obese RR regression results diabetic cascade univariable reg treated.csv")

obese_reg <-summary(obese)
write.csv(obese_reg, "obese p-Value diabetic cascade univariable reg treated.csv")

tobacco_smoked <- glm.cluster(formula = diabetic_treated_dbl ~ tobacco_smoked + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
tobacco_smokedsheet <- exp(cbind(RR = coef(tobacco_smoked), confint(tobacco_smoked)))
write.csv(tobacco_smokedsheet, "tobacco_smoked RR regression results diabetic cascade univariable reg treated.csv")

tobacco_smoked_reg <-summary(tobacco_smoked)
write.csv(tobacco_smoked_reg, "tobacco_smoked p-Value diabetic cascade univariable reg treated.csv")

ex_htn_broad_ind <- glm.cluster(formula = diabetic_treated_dbl ~ ex_htn_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
ex_htn_broad_indsheet <- exp(cbind(RR = coef(ex_htn_broad_ind), confint(ex_htn_broad_ind)))
write.csv(ex_htn_broad_indsheet, "ex_htn_broad_ind RR regression results htn cascade univariable reg treated.csv")

ex_htn_broad_ind_reg <-summary(ex_htn_broad_ind)
write.csv(ex_htn_broad_ind_reg, "ex_htn_broad_ind p-Value htn cascade univariable reg treated.csv")

###Controlled

obese <- glm.cluster(formula = diabetic_controlled_dbl ~ obese + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
obesesheet <- exp(cbind(RR = coef(obese), confint(obese)))
write.csv(obesesheet, "obese RR regression results diabetic cascade univariable reg controlled.csv")

obese_reg <-summary(obese)
write.csv(obese_reg, "obese p-Value diabetic cascade univariable reg controlled.csv")

tobacco_smoked <- glm.cluster(formula = diabetic_controlled_dbl ~ tobacco_smoked + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
tobacco_smokedsheet <- exp(cbind(RR = coef(tobacco_smoked), confint(tobacco_smoked)))
write.csv(tobacco_smokedsheet, "tobacco_smoked RR regression results diabetic cascade univariable reg controlled.csv")

tobacco_smoked_reg <-summary(tobacco_smoked)
write.csv(tobacco_smoked_reg, "tobacco_smoked p-Value diabetic cascade univariable reg controlled.csv")

ex_htn_broad_ind <- glm.cluster(formula = diabetic_controlled_dbl ~ ex_htn_broad_ind + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))
ex_htn_broad_indsheet <- exp(cbind(RR = coef(ex_htn_broad_ind), confint(ex_htn_broad_ind)))
write.csv(ex_htn_broad_indsheet, "ex_htn_broad_ind RR regression results htn cascade univariable reg controlled.csv")

ex_htn_broad_ind_reg <-summary(ex_htn_broad_ind)
write.csv(ex_htn_broad_ind_reg, "ex_htn_broad_ind p-Value htn cascade univariable reg controlled.csv")







```




```{r regressions both diabetes and htn POISSON TOBACCO SMOKED UND SMOKELESS}


##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1) 


##Diabetes aware

diabetes_aware <- glm.cluster(formula = both_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))

write.csv(aware, "RR both diseases both aware.csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value both diseases both aware.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR both diseases both aware.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


write.csv(aware, "OR diabetes aware .csv")



results_aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value both diseases both aware.csv")

results_aware <- mutate(results_aware,
                  p_Value = round(Pr...z..,3))

results_aware$p_Value <- sprintf("%.3f", results_aware$p_Value)



results_aware <- mutate(results_aware,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_aware <- results_aware %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_aware, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes aware"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes both diseases both aware.csv")
 
 #####END FORMATTING
 
 
 

##Diabetes treated

diabetes_treated <- glm.cluster(formula = both_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless+ d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "RR both diseases both treated poisson.csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value both diseases both treatedpoisson.csv")

######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR both diseases both treated poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value both diseases both treatedpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "both diseases both treated"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes aware single disease both diseases both treated.csv")
 
 #####END FORMATTING
 

##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = both_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "RR both diseases both controlled poisson.csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value both diseases both controlledpoisson.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR both diseases both controlled poisson.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value both diseases both controlledpoisson.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "both diseases both controlled"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "diabetes aware single disease both diseases both controlled.csv")
 



```







```{r regressions diabetes cascade poisson number comorbidities}


##filter



dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1) 

dhs_nomiss_diabetic_only$sum_multidiab <- as.factor(dhs_nomiss_diabetic_only$sum_multidiab)
##Diabetes aware

diabetes_aware <- glm.cluster(formula = diabetic_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + sum_multidiab + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
write.csv(aware, "RR diabetes aware poisson number comorbidities.csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes awarepoisson number comorbidities.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes aware poisson number comorbidities.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes awarepoisson number comorbidities.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes aware"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL diabetes aware poisson number comorbidities.csv.csv")
 
```


```{r state prevalences######################################################}




dhs_nomiss_noNAinpsu <- mutate(dhs_nomiss, 
                     psu = ifelse(psu==1, NA, psu))

summary(dhs_nomiss_htn_only$psu)

dhs_nomiss_noNAinpsu <- filter(dhs_nomiss, is.na(psu)==F)

summary(dhs_nomiss_noNAinpsu$psu)

sum(is.na(dhs_nomiss$psu)==T)



#### Crude aware htn prev by state


svy_aware <- dhs_nomiss_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_aware_dbl, ex_state_ind))

prevtot <- svy_aware %>%
  group_by(ex_state_ind) %>% 
  summarize(htn_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(htn_aware = 100*htn_aware_prop,
         htn_low = 100*htn_aware_prop_low,
         htn_upp = 100*htn_aware_prop_upp)


write_csv(prevtot, "stateprevawarebystate_2018-03-08.csv")

#### Crude treated htn prev by state


svy_treated <- dhs_nomiss_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, ex_state_ind))

prevtot <- svy_treated %>%
  group_by(ex_state_ind) %>% 
  summarize(htn_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(htn_treated = 100*htn_treated_prop,
         htn_low = 100*htn_treated_prop_low,
         htn_upp = 100*htn_treated_prop_upp)


write_csv(prevtot, "stateprevtreatedbystate_2018-03-08.csv")

#### Crude controlled htn prev by state


svy_controlled <- dhs_nomiss_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_controlled_dbl, ex_state_ind))

prevtot <- svy_controlled %>%
  group_by(ex_state_ind) %>% 
  summarize(htn_controlled_prop = survey_mean(htn_controlled_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(htn_controlled = 100*htn_controlled_prop,
         htn_low = 100*htn_controlled_prop_low,
         htn_upp = 100*htn_controlled_prop_upp)


write_csv(prevtot, "stateprevcontrolledbystate_2018-03-08.csv")


```

```{r STATE MAPS WITH R AWARE}


#  SET-UP  #


# http://stackoverflow.com/questions/28322866/mapping-just-one-state-of-india-and-writing-its-name-inside-the-state-boundary
library(rgeos)
library(rgdal)
library(raster) # get data for maps

india <- getData("GADM", country = "India", level = 1)
dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1)
india <- gSimplify(india, tol=0.01, topologyPreserve=TRUE) # this drastically reduces the detail in the GADM file to allow for decently quick plotting
map <- fortify(india) # makes a dataset out of a spatial object
map$id <- as.integer(map$id) # that's just to be able to merge it

#dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1) # This is just a df of state names and state IDs
dat <- filter(dat, 
              row_number() != 31) # This just removes the Tamil Nadu duplicate (for some reason the india spatial data has a separate row for Madras as for TN)


centers <- data.frame(gCentroid(india, byid = TRUE)) # a df of latitude and longitude
centers <- filter(centers, 
                  row_number() !=31)  # This is removing the Tamil Nadu duplicate
centers$state <- as.factor(dat$state)  # adding state names to it
centers <- as_tibble(centers)

# Abbreviating the state names and throwing out Lakshadweep and Dadra, Nagar Haveli, D&D, Chandigarh, and Puducherry

centers <- centers %>% 
  mutate(state = fct_recode(state, 
                                     "HP" = "Himachal Pradesh",
                                     "PB" = "Punjab",
                                     "Chandigarh" = "Chandigarh",
                                     "HR" = "Haryana",
                                     "DL" = "NCT of Delhi",
                                     "SK" = "Sikkim",
                                     "Daman and Diu" = "Daman and Diu",
                                     "AR" = "Arunachal Pradesh",
                                     "NL" = "Nagaland",
                                     "MN" = "Manipur",
                                     "MZ" = "Mizoram",
                                     "TR" = "Tripura",
                                     "ML" = "Meghalaya",
                                     "WB" = "West Bengal",
                                     "MH" = "Maharashtra",
                                     "AP" = "Andhra Pradesh",
                                     "KA" = "Karnataka",
                                     "GA" = "Goa",
                                     "KL" = "Kerala",
                                     "Puducherry" = "Puducherry",
                                     "TN" = "Tamil Nadu",
                                     "AN" = "Andaman and Nicobar",
                                     "TS" = "Telangana",
                                     "UK" = "Uttarakhand",
                                     "RJ" = "Rajasthan",
                                     "UP" = "Uttar Pradesh",
                                     "BR" = "Bihar",
                                     "AS" = "Assam",
                                     "JH" = "Jharkhand",
                                     "OD" = "Odisha",
                                     "CT" = "Chhattisgarh", 
                                     "MP" = "Madhya Pradesh",
                                     "JK" = "Jammu and Kashmir",
                                     "GJ" = "Gujarat",
                                     "Lakshadweep" = "Lakshadweep",
                                "Dadra and Nagar Haveli" = "Dadra and Nagar Haveli")) %>%
                  filter(state != "Lakshadweep" & state != "Dadra and Nagar Haveli" & state != "Chandigarh" & state != "Daman and Diu" & state != "Puducherry" )

centers <- centers %>% 
  mutate(ex_state_ind = state)

theme_map <- function (base_size = 12, base_family = "") {
theme_gray(base_size = base_size, base_family = base_family) %+replace% 
theme(
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.ticks.length=unit(0.3, "lines"),
axis.ticks.margin=unit(0.5, "lines"),  # deprecated
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.background=element_rect(fill="white", colour=NA),
legend.key=element_rect(colour="white"),
legend.key.size=unit(1.5, "lines"),
legend.position="right",
legend.text=element_text(size=15, family="Times"),
legend.title=element_blank(),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin=unit(0, "lines"),  # deprecated
plot.background=element_blank(),
plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
plot.title=element_text(size=rel(1.8), face="bold", hjust=0.5, family="Times"),
strip.background=element_rect(fill="white", colour="white"),
strip.text=element_text(size=rel(1.4), face="italic", family="Times")
)   
}


both_noob_nosmo <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)


# Now calculate prevalence by state
temp.dat2 <- both_noob_nosmo_noNAinpsu %>% 
  group_by(ex_state_ind) %>%
  mutate(multi_risc = 100*weighted.mean(both_aware_dbl,p_wt_new, na.rm=TRUE)) %>% 
  filter(row_number()==1) %>% 
  dplyr::select(ex_state_ind, multi_risc) %>% 
  filter(ex_state_ind!="Daman and Diu")  # Daman and Diu has a crazy high urban prev, so to not distort color scale kick out this invisible state


dat <- dat %>% 
  mutate(ex_state_ind = state) %>%
   mutate(ex_state_ind = fct_recode(ex_state_ind, 
                                     "Andaman and Nicobar Islands" = "Andaman and Nicobar",
                                     "Delhi" = "NCT of Delhi")) %>%
  filter(ex_state_ind!="Daman and Diu") 


dat$ex_state_ind <- factor(dat$ex_state_ind, levels = c("Andaman and Nicobar Islands",
                                                        "Andhra Pradesh",
                                                        "Arunachal Pradesh",
                                                        "Assam",
                                                        "Bihar",
"Chandigarh",
"Chhattisgarh",
"Dadra and Nagar Haveli",
"Goa",
"Gujarat",
"Haryana",
"Himachal Pradesh",
"Jammu and Kashmir",
"Jharkhand",
"Karnataka",
"Kerala",
"Lakshadweep",
"Madhya Pradesh",
"Maharashtra",
"Manipur",
"Meghalaya",
"Mizoram",
"Nagaland",
"Delhi",
"Odisha",
"Puducherry",
"Punjab",
"Rajasthan",
"Sikkim",
"Tamil Nadu",
"Telangana",
"Tripura",
"Uttar Pradesh",
"Uttarakhand",
"West Bengal"))

temp.dat2$ex_state_ind <- factor(temp.dat2$ex_state_ind, levels = c("Andaman and Nicobar Islands",
                                                        "Andhra Pradesh",
                                                        "Arunachal Pradesh",
                                                        "Assam",
                                                        "Bihar",
"Chandigarh",
"Chhattisgarh",
"Dadra and Nagar Haveli",
"Goa",
"Gujarat",
"Haryana",
"Himachal Pradesh",
"Jammu and Kashmir",
"Jharkhand",
"Karnataka",
"Kerala",
"Lakshadweep",
"Madhya Pradesh",
"Maharashtra",
"Manipur",
"Meghalaya",
"Mizoram",
"Nagaland",
"Delhi",
"Odisha",
"Puducherry",
"Punjab",
"Rajasthan",
"Sikkim",
"Tamil Nadu",
"Telangana",
"Tripura",
"Uttar Pradesh",
"Uttarakhand",
"West Bengal"))


map.dat2 <- left_join(dat, temp.dat2, by="ex_state_ind") # adds an id column to cvd_tempdat
map.dat2 <- inner_join(map, map.dat2, by = "id")


# Now plot the actual map - htn
htn_map2 <- ggplot() +
  geom_map(data=map.dat2, map = map.dat2,
         aes(map_id = id, group = group),
         color = "#ffffff", fill = "#ececec", size = 0.25) +
  geom_map(data=map.dat2, map = map.dat2,
         aes(map_id = id, group = group, fill=multi_risc),
         color = "#ffffff", size = 0.25) +
  geom_text_repel(data = centers, 
                  aes(label = ex_state_ind, x = x, y = y, fontface=2), 
                  size = 7, segment.color = "black", segment.size = 0.3, family="Times") +
  coord_map() +
  scale_fill_distiller(palette = "YlGn", direction = 1, na.value = "grey80") +
  labs(x = "", y = "") +
  xlim(68, 98) + 
  ylim(7, 35) +
  ggtitle("Aware (%)") + 
  theme_map()
htn_map2

```



```{r STATE MAPS WITH R TREATED}


#  SET-UP  #


# http://stackoverflow.com/questions/28322866/mapping-just-one-state-of-india-and-writing-its-name-inside-the-state-boundary
library(rgeos)
library(rgdal)
library(raster) # get data for maps

india <- getData("GADM", country = "India", level = 1)
dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1)
india <- gSimplify(india, tol=0.01, topologyPreserve=TRUE) # this drastically reduces the detail in the GADM file to allow for decently quick plotting
map <- fortify(india) # makes a dataset out of a spatial object
map$id <- as.integer(map$id) # that's just to be able to merge it

#dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1) # This is just a df of state names and state IDs
dat <- filter(dat, 
              row_number() != 31) # This just removes the Tamil Nadu duplicate (for some reason the india spatial data has a separate row for Madras as for TN)


centers <- data.frame(gCentroid(india, byid = TRUE)) # a df of latitude and longitude
centers <- filter(centers, 
                  row_number() !=31)  # This is removing the Tamil Nadu duplicate
centers$state <- as.factor(dat$state)  # adding state names to it
centers <- as_tibble(centers)

# Abbreviating the state names and throwing out Lakshadweep and Dadra, Nagar Haveli, D&D, Chandigarh, and Puducherry

centers <- centers %>% 
  mutate(state = fct_recode(state, 
                                     "HP" = "Himachal Pradesh",
                                     "PB" = "Punjab",
                                     "Chandigarh" = "Chandigarh",
                                     "HR" = "Haryana",
                                     "DL" = "NCT of Delhi",
                                     "SK" = "Sikkim",
                                     "Daman and Diu" = "Daman and Diu",
                                     "AR" = "Arunachal Pradesh",
                                     "NL" = "Nagaland",
                                     "MN" = "Manipur",
                                     "MZ" = "Mizoram",
                                     "TR" = "Tripura",
                                     "ML" = "Meghalaya",
                                     "WB" = "West Bengal",
                                     "MH" = "Maharashtra",
                                     "AP" = "Andhra Pradesh",
                                     "KA" = "Karnataka",
                                     "GA" = "Goa",
                                     "KL" = "Kerala",
                                     "Puducherry" = "Puducherry",
                                     "TN" = "Tamil Nadu",
                                     "AN" = "Andaman and Nicobar",
                                     "TS" = "Telangana",
                                     "UK" = "Uttarakhand",
                                     "RJ" = "Rajasthan",
                                     "UP" = "Uttar Pradesh",
                                     "BR" = "Bihar",
                                     "AS" = "Assam",
                                     "JH" = "Jharkhand",
                                     "OD" = "Odisha",
                                     "CT" = "Chhattisgarh", 
                                     "MP" = "Madhya Pradesh",
                                     "JK" = "Jammu and Kashmir",
                                     "GJ" = "Gujarat",
                                     "Lakshadweep" = "Lakshadweep",
                                "Dadra and Nagar Haveli" = "Dadra and Nagar Haveli")) %>%
                  filter(state != "Lakshadweep" & state != "Dadra and Nagar Haveli" & state != "Chandigarh" & state != "Daman and Diu" & state != "Puducherry" )

centers <- centers %>% 
  mutate(ex_state_ind = state)

theme_map <- function (base_size = 12, base_family = "") {
theme_gray(base_size = base_size, base_family = base_family) %+replace% 
theme(
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.ticks.length=unit(0.3, "lines"),
axis.ticks.margin=unit(0.5, "lines"),  # deprecated
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.background=element_rect(fill="white", colour=NA),
legend.key=element_rect(colour="white"),
legend.key.size=unit(1.5, "lines"),
legend.position="right",
legend.text=element_text(size=15, family="Times"),
legend.title=element_blank(),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin=unit(0, "lines"),  # deprecated
plot.background=element_blank(),
plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
plot.title=element_text(size=rel(1.8), face="bold", hjust=0.5, family="Times"),
strip.background=element_rect(fill="white", colour="white"),
strip.text=element_text(size=rel(1.4), face="italic", family="Times")
)   
}



both_noob_nosmo <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)



# Now calculate prevalence by state
temp.dat2 <- both_noob_nosmo_noNAinpsu %>% 
  group_by(ex_state_ind) %>%
  mutate(multi_risc = 100*weighted.mean(both_treated_dbl,p_wt_new, na.rm=TRUE)) %>% 
  filter(row_number()==1) %>% 
  dplyr::select(ex_state_ind, multi_risc) %>% 
  filter(ex_state_ind!="Daman and Diu")  # Daman and Diu has a crazy high urban prev, so to not distort color scale kick out this invisible state


dat <- dat %>% 
  mutate(ex_state_ind = state) %>%
   mutate(ex_state_ind = fct_recode(ex_state_ind, 
                                     "Andaman and Nicobar Islands" = "Andaman and Nicobar",
                                     "Delhi" = "NCT of Delhi")) %>%
  filter(ex_state_ind!="Daman and Diu") 


dat$ex_state_ind <- factor(dat$ex_state_ind, levels = c("Andaman and Nicobar Islands",
                                                        "Andhra Pradesh",
                                                        "Arunachal Pradesh",
                                                        "Assam",
                                                        "Bihar",
"Chandigarh",
"Chhattisgarh",
"Dadra and Nagar Haveli",
"Goa",
"Gujarat",
"Haryana",
"Himachal Pradesh",
"Jammu and Kashmir",
"Jharkhand",
"Karnataka",
"Kerala",
"Lakshadweep",
"Madhya Pradesh",
"Maharashtra",
"Manipur",
"Meghalaya",
"Mizoram",
"Nagaland",
"Delhi",
"Odisha",
"Puducherry",
"Punjab",
"Rajasthan",
"Sikkim",
"Tamil Nadu",
"Telangana",
"Tripura",
"Uttar Pradesh",
"Uttarakhand",
"West Bengal"))

temp.dat2$ex_state_ind <- factor(temp.dat2$ex_state_ind, levels = c("Andaman and Nicobar Islands",
                                                        "Andhra Pradesh",
                                                        "Arunachal Pradesh",
                                                        "Assam",
                                                        "Bihar",
"Chandigarh",
"Chhattisgarh",
"Dadra and Nagar Haveli",
"Goa",
"Gujarat",
"Haryana",
"Himachal Pradesh",
"Jammu and Kashmir",
"Jharkhand",
"Karnataka",
"Kerala",
"Lakshadweep",
"Madhya Pradesh",
"Maharashtra",
"Manipur",
"Meghalaya",
"Mizoram",
"Nagaland",
"Delhi",
"Odisha",
"Puducherry",
"Punjab",
"Rajasthan",
"Sikkim",
"Tamil Nadu",
"Telangana",
"Tripura",
"Uttar Pradesh",
"Uttarakhand",
"West Bengal"))


map.dat2 <- left_join(dat, temp.dat2, by="ex_state_ind") # adds an id column to cvd_tempdat
map.dat2 <- inner_join(map, map.dat2, by = "id")


# Now plot the actual map - htn
htn_map2 <- ggplot() +
  geom_map(data=map.dat2, map = map.dat2,
         aes(map_id = id, group = group),
         color = "#ffffff", fill = "#ececec", size = 0.25) +
  geom_map(data=map.dat2, map = map.dat2,
         aes(map_id = id, group = group, fill=multi_risc),
         color = "#ffffff", size = 0.25) +
  geom_text_repel(data = centers, 
                  aes(label = ex_state_ind, x = x, y = y, fontface=2), 
                  size = 7, segment.color = "black", segment.size = 0.3, family="Times") +
  coord_map() +
  scale_fill_distiller(palette = "YlGn", direction = 1, na.value = "grey80") +
  labs(x = "", y = "") +
  xlim(68, 98) + 
  ylim(7, 35) +
  ggtitle("Treated (%)") + 
  theme_map()
htn_map2

```


```{r STATE MAPS WITH R Controlled}


#  SET-UP  #


# http://stackoverflow.com/questions/28322866/mapping-just-one-state-of-india-and-writing-its-name-inside-the-state-boundary
library(rgeos)
library(rgdal)
library(raster) # get data for maps

india <- getData("GADM", country = "India", level = 1)
dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1)
india <- gSimplify(india, tol=0.01, topologyPreserve=TRUE) # this drastically reduces the detail in the GADM file to allow for decently quick plotting
map <- fortify(india) # makes a dataset out of a spatial object
map$id <- as.integer(map$id) # that's just to be able to merge it

#dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1) # This is just a df of state names and state IDs
dat <- filter(dat, 
              row_number() != 31) # This just removes the Tamil Nadu duplicate (for some reason the india spatial data has a separate row for Madras as for TN)


centers <- data.frame(gCentroid(india, byid = TRUE)) # a df of latitude and longitude
centers <- filter(centers, 
                  row_number() !=31)  # This is removing the Tamil Nadu duplicate
centers$state <- as.factor(dat$state)  # adding state names to it
centers <- as_tibble(centers)

# Abbreviating the state names and throwing out Lakshadweep and Dadra, Nagar Haveli, D&D, Chandigarh, and Puducherry

centers <- centers %>% 
  mutate(state = fct_recode(state, 
                                     "HP" = "Himachal Pradesh",
                                     "PB" = "Punjab",
                                     "Chandigarh" = "Chandigarh",
                                     "HR" = "Haryana",
                                     "DL" = "NCT of Delhi",
                                     "SK" = "Sikkim",
                                     "Daman and Diu" = "Daman and Diu",
                                     "AR" = "Arunachal Pradesh",
                                     "NL" = "Nagaland",
                                     "MN" = "Manipur",
                                     "MZ" = "Mizoram",
                                     "TR" = "Tripura",
                                     "ML" = "Meghalaya",
                                     "WB" = "West Bengal",
                                     "MH" = "Maharashtra",
                                     "AP" = "Andhra Pradesh",
                                     "KA" = "Karnataka",
                                     "GA" = "Goa",
                                     "KL" = "Kerala",
                                     "Puducherry" = "Puducherry",
                                     "TN" = "Tamil Nadu",
                                     "AN" = "Andaman and Nicobar",
                                     "TS" = "Telangana",
                                     "UK" = "Uttarakhand",
                                     "RJ" = "Rajasthan",
                                     "UP" = "Uttar Pradesh",
                                     "BR" = "Bihar",
                                     "AS" = "Assam",
                                     "JH" = "Jharkhand",
                                     "OD" = "Odisha",
                                     "CT" = "Chhattisgarh", 
                                     "MP" = "Madhya Pradesh",
                                     "JK" = "Jammu and Kashmir",
                                     "GJ" = "Gujarat",
                                     "Lakshadweep" = "Lakshadweep",
                                "Dadra and Nagar Haveli" = "Dadra and Nagar Haveli")) %>%
                  filter(state != "Lakshadweep" & state != "Dadra and Nagar Haveli" & state != "Chandigarh" & state != "Daman and Diu" & state != "Puducherry" )

centers <- centers %>% 
  mutate(ex_state_ind = state)

theme_map <- function (base_size = 12, base_family = "") {
theme_gray(base_size = base_size, base_family = base_family) %+replace% 
theme(
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.ticks.length=unit(0.3, "lines"),
axis.ticks.margin=unit(0.5, "lines"),  # deprecated
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.background=element_rect(fill="white", colour=NA),
legend.key=element_rect(colour="white"),
legend.key.size=unit(1.5, "lines"),
legend.position="right",
legend.text=element_text(size=15, family="Times"),
legend.title=element_blank(),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin=unit(0, "lines"),  # deprecated
plot.background=element_blank(),
plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
plot.title=element_text(size=rel(1.8), face="bold", hjust=0.5, family="Times"),
strip.background=element_rect(fill="white", colour="white"),
strip.text=element_text(size=rel(1.4), face="italic", family="Times")
)   
}




both_noob_nosmo <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)

###create PSU ID in both_noob_nosmo :
both_noob_nosmo <- both_noob_nosmo %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
both_noob_nosmo$psuid <- as.factor(both_noob_nosmo$psuid)

###make noNAinpsu

both_noob_nosmo_noNAinpsu <- filter(both_noob_nosmo, is.na(psu)==F)

summary(both_noob_nosmo_noNAinpsu$psu)

sum(is.na(both_noob_nosmo_noNAinpsu$psu)==T)





# Now calculate prevalence by state
temp.dat2 <- both_noob_nosmo_noNAinpsu %>% 
  group_by(ex_state_ind) %>%
  mutate(multi_risc = 100*weighted.mean(both_controlled_dbl,p_wt_new, na.rm=TRUE)) %>% 
  filter(row_number()==1) %>% 
  dplyr::select(ex_state_ind, multi_risc) %>% 
  filter(ex_state_ind!="Daman and Diu")  # Daman and Diu has a crazy high urban prev, so to not distort color scale kick out this invisible state


dat <- dat %>% 
  mutate(ex_state_ind = state) %>%
   mutate(ex_state_ind = fct_recode(ex_state_ind, 
                                     "Andaman and Nicobar Islands" = "Andaman and Nicobar",
                                     "Delhi" = "NCT of Delhi")) %>%
  filter(ex_state_ind!="Daman and Diu") 


dat$ex_state_ind <- factor(dat$ex_state_ind, levels = c("Andaman and Nicobar Islands",
                                                        "Andhra Pradesh",
                                                        "Arunachal Pradesh",
                                                        "Assam",
                                                        "Bihar",
"Chandigarh",
"Chhattisgarh",
"Dadra and Nagar Haveli",
"Goa",
"Gujarat",
"Haryana",
"Himachal Pradesh",
"Jammu and Kashmir",
"Jharkhand",
"Karnataka",
"Kerala",
"Lakshadweep",
"Madhya Pradesh",
"Maharashtra",
"Manipur",
"Meghalaya",
"Mizoram",
"Nagaland",
"Delhi",
"Odisha",
"Puducherry",
"Punjab",
"Rajasthan",
"Sikkim",
"Tamil Nadu",
"Telangana",
"Tripura",
"Uttar Pradesh",
"Uttarakhand",
"West Bengal"))

temp.dat2$ex_state_ind <- factor(temp.dat2$ex_state_ind, levels = c("Andaman and Nicobar Islands",
                                                        "Andhra Pradesh",
                                                        "Arunachal Pradesh",
                                                        "Assam",
                                                        "Bihar",
"Chandigarh",
"Chhattisgarh",
"Dadra and Nagar Haveli",
"Goa",
"Gujarat",
"Haryana",
"Himachal Pradesh",
"Jammu and Kashmir",
"Jharkhand",
"Karnataka",
"Kerala",
"Lakshadweep",
"Madhya Pradesh",
"Maharashtra",
"Manipur",
"Meghalaya",
"Mizoram",
"Nagaland",
"Delhi",
"Odisha",
"Puducherry",
"Punjab",
"Rajasthan",
"Sikkim",
"Tamil Nadu",
"Telangana",
"Tripura",
"Uttar Pradesh",
"Uttarakhand",
"West Bengal"))


map.dat2 <- left_join(dat, temp.dat2, by="ex_state_ind") # adds an id column to cvd_tempdat
map.dat2 <- inner_join(map, map.dat2, by = "id")


# Now plot the actual map - htn
htn_map2 <- ggplot() +
  geom_map(data=map.dat2, map = map.dat2,
         aes(map_id = id, group = group),
         color = "#ffffff", fill = "#ececec", size = 0.25) +
  geom_map(data=map.dat2, map = map.dat2,
         aes(map_id = id, group = group, fill=multi_risc),
         color = "#ffffff", size = 0.25) +
  geom_text_repel(data = centers, 
                  aes(label = ex_state_ind, x = x, y = y, fontface=2), 
                  size = 7, segment.color = "black", segment.size = 0.3, family="Times") +
  coord_map() +
  scale_fill_distiller(palette = "Greens", direction = 1, na.value = "grey80") +
  labs(x = "", y = "") +
  xlim(68, 98) + 
  ylim(7, 35) +
  ggtitle("Controlled (%)") + 
  theme_map()
htn_map2

```



```{r}

##Diabetes treated

diabetes_treated <- glm.cluster(formula = diabetic_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + sum_multidiab + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "RR diabetes treated poisson number comorbidities.csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treatedpoisson number comorbidities.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes treated poisson number comorbidities.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treatedpoisson number comorbidities.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes treated"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL diabetes treatedpoisson number comorbidities.csv")
 

```

```{r}

##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = diabetic_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + sum_multidiab + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "RR diabetes controlled poisson number comorbidities.csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlledpoisson number comorbidities.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes controlled poisson number comorbidities.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlledpoisson number comorbidities.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes controlled"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL diabetes controlledpoisson number comorbidities.csv")
 

```


```{r regressions diabetes cascade poisson with hiv among hiv dataset number comorbidities}


##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & is.na(hiv01)==F) 

dhs_nomiss_diabetic_only$sum_multidiab <- as.factor(dhs_nomiss_diabetic_only$sum_multidiab)
##Diabetes aware

diabetes_aware <- glm.cluster(formula = diabetic_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + sum_multidiab + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
write.csv(aware, "RR diabetes aware poisson hiv number comorbidities.csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes awarepoisson hiv number comorbidities.csv")

##Diabetes treated

diabetes_treated <- glm.cluster(formula = diabetic_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + sum_multidiab + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "RR diabetes treated poisson hiv number comorbidities.csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treatedpoisson hiv number comorbidities.csv")

##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = diabetic_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + sum_multidiab + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "RR diabetes controlled poisson hiv number comorbidities.csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlledpoisson hiv number comorbidities.csv")




```







```{r regressions diabetes cascade poisson treated vs untreated}


##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1) 

dhs_nomiss_diabetic_only_noNAtreated <- filter(dhs_nomiss_diabetic_only, is.na(hypt_med)==F & is.na(asthma_treated)==F & is.na(thyroid_treated)==F & is.na(heart_treated)==F & is.na(cancer_treated)==F)

dhs_nomiss_diabetic_only<- mutate(dhs_nomiss_diabetic_only, 
              htn_untreated = ifelse(ex_htn_broad_ind_dbl==1 & hypt_med==0,1,0),
              asthma_untreated = ifelse(asthma==1 & asthma_treated==0,1,0),
              thyroid_untreated = ifelse(thyroid==1 & thyroid_treated==0,1,0),
               heart_untreated = ifelse(heart==1 & heart_treated==0,1,0),
               cancer_untreated = ifelse(cancer==1 & cancer_treated==0,1,0))

dhs_nomiss_diabetic_only<- mutate(dhs_nomiss_diabetic_only, 
              htn_treated = ifelse(ex_htn_broad_ind_dbl==1 & hypt_med==1,1,0),
              asthma_treated = ifelse(asthma==1 & asthma_treated==1,1,0),
              thyroid_treated = ifelse(thyroid==1 & thyroid_treated==1,1,0),
               heart_treated = ifelse(heart==1 & heart_treated==1,1,0),
               cancer_treated = ifelse(cancer==1 & cancer_treated==1,1,0))
                                   

   

##Diabetes aware

diabetes_aware <- glm.cluster(formula = diabetic_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + htn_treated + htn_untreated  + asthma_treated + asthma_untreated  + thyroid_treated + thyroid_untreated  + heart_treated + heart_untreated  +  cancer_treated + cancer_untreated + d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
write.csv(aware, "RR diabetes aware poisson treated vs untreated.csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes awarepoisson treated vs untreated.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes aware poisson treated vs untreated.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes awarepoisson treated vs untreated.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes aware"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL diabetes diabetes awarepoisson treated vs untreated.csv")
 
 
```


```{r}

##Diabetes treated

diabetes_treated <- glm.cluster(formula = diabetic_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless +  htn_treated + htn_untreated + asthma_treated + asthma_untreated + thyroid_treated + thyroid_untreated + heart_treated + heart_untreated +  cancer_treated + cancer_untreated + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "RR diabetes treated poisson treated vs untreated.csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treatedpoisson treated vs untreated.csv")



######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes treated poisson treated vs untreated.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes treatedpoisson treated vs untreated.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes treated"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL diabetesdiabetes treatedpoisson treated vs untreated.csv")
 
```


```{r}
 
 
##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = diabetic_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + htn_treated + htn_untreated + asthma_treated + asthma_untreated + thyroid_treated + thyroid_untreated + heart_treated + heart_untreated +  cancer_treated + cancer_untreated  + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "RR diabetes controlled poisson treated vs untreated.csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlledpoisson treated vs untreated.csv")


######FORMATTING


aware <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/RR diabetes controlled poisson treated vs untreated.csv")



colnames(aware)[colnames(aware)=="X2.5.."] <- "lowci"
colnames(aware)[colnames(aware)=="X97.5.."] <- "uppci"

 aware <- mutate(aware,
                  lowci = round(lowci,2),
                  RR =  round(RR,2),
                  uppci =  round(uppci,2))
 

aware$lowci <- sprintf("%.2f", aware$lowci)
aware$RR <- sprintf("%.2f", aware$RR)
aware$uppci <- sprintf("%.2f", aware$uppci)



  aware <- mutate(aware,
        citemp = str_c(lowci, uppci, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rr = str_c(RR, ci, sep=" ")) 
  
  aware <- aware %>%
      dplyr::select(X,rr)


results_treated <- read.csv("~/Documents/Public Health Files/Public Health/public health/cascades with morbidities/p Value diabetes controlledpoisson treated vs untreated.csv")

results_treated <- mutate(results_treated,
                  p_Value = round(Pr...z..,3))

results_treated$p_Value <- sprintf("%.3f", results_treated$p_Value)



results_treated <- mutate(results_treated,
                  p_Value = ifelse(p_Value=="0.000", "<0.001", p_Value))

  results_treated <- results_treated %>%
      dplyr::select(X,p_Value)
  
  joint <- left_join(aware, results_treated, by=c("X"="X"))

  
  
age <- c(" ", "1 (Reference)"," ")
noref <- c(" ", " "," ")
diab <- c(" ", "Diabetes controlled"," ")

joint <- InsertRow(joint, NewRow = noref, RowNum = 2)
joint <- InsertRow(joint, NewRow = age, RowNum = 3)
joint <- InsertRow(joint, NewRow = noref, RowNum = 11)
joint <- InsertRow(joint, NewRow = age, RowNum = 12)
joint <- InsertRow(joint, NewRow = noref, RowNum = 17)
joint <- InsertRow(joint, NewRow = age, RowNum = 18)
joint <- InsertRow(joint, NewRow = noref, RowNum = 25)
joint <- InsertRow(joint, NewRow = age, RowNum = 26)
joint <- InsertRow(joint, NewRow = noref, RowNum = 34)
joint <- InsertRow(joint, NewRow = age, RowNum = 35)
joint <- InsertRow(joint, NewRow = diab, RowNum = 1)


 write.csv(joint, "FINAL diabetesdiabetes controlledpoisson treated vs untreated.csv")
 

```



```{r regressions diabetes cascade poisson with hiv among hiv dataset treated vs untreated}


##filter
dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1 & is.na(hiv01)==F) 


dhs_nomiss_diabetic_only<- mutate(dhs_nomiss_diabetic_only, 
              htn_untreated = ifelse(ex_htn_broad_ind_dbl==1 & hypt_med==0,1,0),
              asthma_untreated = ifelse(asthma==1 & asthma_treated==0,1,0),
              thyroid_untreated = ifelse(thyroid==1 & thyroid_treated==0,1,0),
               heart_untreated = ifelse(heart==1 & heart_treated==0,1,0),
               cancer_untreated = ifelse(cancer==1 & cancer_treated==0,1,0))

dhs_nomiss_diabetic_only<- mutate(dhs_nomiss_diabetic_only, 
              htn_treated = ifelse(ex_htn_broad_ind_dbl==1 & hypt_med==1,1,0),
              asthma_treated = ifelse(asthma==1 & asthma_treated==1,1,0),
              thyroid_treated = ifelse(thyroid==1 & thyroid_treated==1,1,0),
               heart_treated = ifelse(heart==1 & heart_treated==1,1,0),
               cancer_treated = ifelse(cancer==1 & cancer_treated==1,1,0))
                                   

##Diabetes aware

diabetes_aware <- glm.cluster(formula = diabetic_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless +htn_treated + htn_untreated + asthma_treated + asthma_untreated + thyroid_treated + thyroid_untreated + heart_treated + heart_untreated +  cancer_treated + cancer_untreated+ d_id ,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
write.csv(aware, "RR diabetes aware poisson hiv treated vs untreated.csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes awarepoisson hiv treated vs untreated.csv")

##Diabetes treated

diabetes_treated <- glm.cluster(formula = diabetic_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + htn_treated + htn_untreated + asthma_treated + asthma_untreated + thyroid_treated + thyroid_untreated + heart_treated + heart_untreated +  cancer_treated + cancer_untreated + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

treated <- exp(cbind(RR = coef(diabetes_treated), confint(diabetes_treated)))
write.csv(treated, "RR diabetes treated poisson hiv treated vs untreated.csv")

results_treated <-summary(diabetes_treated)
write.csv(results_treated, "p Value diabetes treatedpoisson hiv treated vs untreated.csv")

##Diabetes controlled

diabetes_controlled <- glm.cluster(formula = diabetic_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + htn_treated + htn_untreated + asthma_treated + asthma_untreated + thyroid_treated + thyroid_untreated + heart_treated + heart_untreated +  cancer_treated + cancer_untreated + d_id,  cluster="psu", data=dhs_nomiss_diabetic_only, family=poisson(link="log"))

controlled <- exp(cbind(RR = coef(diabetes_controlled), confint(diabetes_controlled)))
write.csv(controlled, "RR diabetes controlled poisson hiv treated vs untreated.csv")

results_controlled <-summary(diabetes_controlled)
write.csv(results_controlled, "p Value diabetes controlledpoisson hiv treated vs untreated.csv")




```




```{r predicted probabilities diabetes cascade aware}

dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1) 



dhs_nomiss_diabetic_only_no4comorb <- filter(dhs_nomiss_diabetic_only, sum_multidiab_dbl<=3)



diabetes_aware <- glm(formula = diabetic_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_multidiab + wealth_quintile_rurb:sum_multidiab + wealth_quintile_rurb:urban + sum_multidiab:urban + d_id , data=dhs_nomiss_diabetic_only_no4comorb, family=binomial(link="logit"))



pred.diab.aware <- filter(dhs_nomiss_diabetic_only_no4comorb, is.na(diabetic_aware_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_multidiab)==F &  is.na(d_id)==F)
pred.diab <- augment(diabetes_aware, pred.diab.aware, type.predict = "response", se.fit = TRUE)

pred.diab <- pred.diab %>% 
  mutate(prob_diab = .fitted,
         se_diab = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_diab <- aggregate(prob_diab ~ wealth_quintile_rurb_lab  + urban_lab + sum_multidiab, data = pred.diab, FUN = mean)
p_standerr_diab <- aggregate(se_diab ~ wealth_quintile_rurb_lab  + urban_lab + sum_multidiab, data = pred.diab, FUN = mean)
fig4.diab <- left_join(pmeans_diab, p_standerr_diab, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_multidiab"))
fig4.diab <- mutate(fig4.diab, 
                   lowerci = prob_diab-(1.96*se_diab),
                   upperci = prob_diab+(1.96*se_diab))


write.csv(fig4.diab, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

diabpredfig <- fig4.diab %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_diab, x=wealth_quintile_rurb_lab, color=sum_multidiab, shape=sum_multidiab),  fun.y="mean",size=3.0, geom="point") +
  #geom_errorbar(aes(ymin=100*lowerci_alt, ymax=100*upperci_alt, x=age_grpOR, color=wealth_quintile_rurb_lab), width=0.3, show.legend = F) +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 4)), name="Number Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17,20,24), name="Number Comorbidities") 
diabpredfig




```




```{r predicted probabilities diabetes cascade treated}


dhs_nomiss_diabetic_only <- filter(dhs_nomiss, ex_diab_broad_ind==1) 


dhs_nomiss_diabetic_only_no4comorb <- dhs_nomiss_diabetic_only


diabetes_treated <- glm(formula = diabetic_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_multidiab + wealth_quintile_rurb:sum_multidiab + wealth_quintile_rurb:urban + sum_multidiab:urban + d_id , data=dhs_nomiss_diabetic_only_no4comorb, family=binomial(link="logit"))



pred.diab.treated <- filter(dhs_nomiss_diabetic_only_no4comorb, is.na(diabetic_treated_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_multidiab)==F &  is.na(d_id)==F)
pred.diab <- augment(diabetes_treated, pred.diab.treated, type.predict = "response", se.fit = TRUE)

pred.diab <- pred.diab %>% 
  mutate(prob_diab = .fitted,
         se_diab = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_diab <- aggregate(prob_diab ~ wealth_quintile_rurb_lab  + urban_lab + sum_multidiab, data = pred.diab, FUN = mean)
p_standerr_diab <- aggregate(se_diab ~ wealth_quintile_rurb_lab  + urban_lab + sum_multidiab, data = pred.diab, FUN = mean)
fig4.diab <- left_join(pmeans_diab, p_standerr_diab, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_multidiab"))
fig4.diab <- mutate(fig4.diab, 
                   lowerci = prob_diab-(1.96*se_diab),
                   upperci = prob_diab+(1.96*se_diab))


write.csv(fig4.diab, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

diabpredfig <- fig4.diab %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_diab, x=wealth_quintile_rurb_lab, color=sum_multidiab, shape=sum_multidiab), fun.y="mean", size=3.0, geom="point") +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 4)), name="Number Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23), name="Number Comorbidities") 
diabpredfig




```


```{r predicted probabilities diabetes cascade controlled}


dhs_nomiss_diabetic_only_no4comorb <- filter(dhs_nomiss_diabetic_only, sum_multidiab_dbl<4)



diabetes_controlled <- glm(formula = diabetic_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_multidiab + wealth_quintile_rurb:sum_multidiab + wealth_quintile_rurb:urban + sum_multidiab:urban + d_id , data=dhs_nomiss_diabetic_only_no4comorb, family=binomial(link="logit"))



pred.diab.controlled <- filter(dhs_nomiss_diabetic_only_no4comorb, is.na(diabetic_controlled_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_multidiab)==F &  is.na(d_id)==F)
pred.diab <- augment(diabetes_controlled, pred.diab.controlled, type.predict = "response", se.fit = TRUE)

pred.diab <- pred.diab %>% 
  mutate(prob_diab = .fitted,
         se_diab = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_diab <- aggregate(prob_diab ~ wealth_quintile_rurb_lab  + urban_lab + sum_multidiab, data = pred.diab, FUN = mean)
p_standerr_diab <- aggregate(se_diab ~ wealth_quintile_rurb_lab  + urban_lab + sum_multidiab, data = pred.diab, FUN = mean)
fig4.diab <- left_join(pmeans_diab, p_standerr_diab, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_multidiab"))
fig4.diab <- mutate(fig4.diab, 
                   lowerci = prob_diab-(1.96*se_diab),
                   upperci = prob_diab+(1.96*se_diab))


write.csv(fig4.diab, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

diabpredfig <- fig4.diab %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_diab, x=wealth_quintile_rurb_lab, color=sum_multidiab, shape=sum_multidiab), fun.y="mean", size=3.0, geom="point") +
  #geom_errorbar(aes(ymin=100*lowerci_alt, ymax=100*upperci_alt, x=age_grpOR, color=wealth_quintile_rurb_lab), width=0.3, show.legend = F) +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 7)), name="Number Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17,20,24), name="Number Comorbidities") 
diabpredfig




```




```{r predicted probabilities diabetes cascade aware treated vs untreated}


dhs_nomiss_diabetic_only_no5treated <- filter(dhs_nomiss_diabetic_only, sum_diab_treated_dbl<5)




diabetes_aware <- glm(formula = diabetic_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_diab_treated + wealth_quintile_rurb:sum_diab_treated + wealth_quintile_rurb:urban + sum_diab_treated:urban + d_id , data=dhs_nomiss_diabetic_only_no5treated, family=binomial(link="logit"))



pred.diab.aware <- filter(dhs_nomiss_diabetic_only_no5treated, is.na(diabetic_aware_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_diab_treated)==F &  is.na(d_id)==F)
pred.diab <- augment(diabetes_aware, pred.diab.aware, type.predict = "response", se.fit = TRUE)

pred.diab <- pred.diab %>% 
  mutate(prob_diab = .fitted,
         se_diab = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_diab <- aggregate(prob_diab ~ wealth_quintile_rurb_lab  + urban_lab + sum_diab_treated, data = pred.diab, FUN = mean)
p_standerr_diab <- aggregate(se_diab ~ wealth_quintile_rurb_lab  + urban_lab + sum_diab_treated, data = pred.diab, FUN = mean)
fig4.diab <- left_join(pmeans_diab, p_standerr_diab, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_diab_treated"))
fig4.diab <- mutate(fig4.diab, 
                   lowerci = prob_diab-(1.96*se_diab),
                   upperci = prob_diab+(1.96*se_diab))


write.csv(fig4.diab, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

diabpredfig <- fig4.diab %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_diab, x=wealth_quintile_rurb_lab, color=sum_diab_treated, shape=sum_diab_treated), fun.y="mean", size=3.0, geom="point") +
  #geom_errorbar(aes(ymin=100*lowerci_alt, ymax=100*upperci_alt, x=age_grpOR, color=wealth_quintile_rurb_lab), width=0.3, show.legend = F) +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 7)), name="Number treated Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17,20,24), name="Number treated Comorbidities") 
diabpredfig




```




```{r predicted probabilities diabetes cascade treated treated vs untreated}


dhs_nomiss_diabetic_only_no5treated <- filter(dhs_nomiss_diabetic_only, sum_diab_treated_dbl<5)

diabetes_treated <- glm(formula = diabetic_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_diab_treated + wealth_quintile_rurb:sum_diab_treated + wealth_quintile_rurb:urban + sum_diab_treated:urban + d_id , data=dhs_nomiss_diabetic_only_no5treated, family=binomial(link="logit"))



pred.diab.treated <- filter(dhs_nomiss_diabetic_only_no5treated, is.na(diabetic_treated_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_diab_treated)==F &  is.na(d_id)==F)
pred.diab <- augment(diabetes_treated, pred.diab.treated, type.predict = "response", se.fit = TRUE)

pred.diab <- pred.diab %>% 
  mutate(prob_diab = .fitted,
         se_diab = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_diab <- aggregate(prob_diab ~ wealth_quintile_rurb_lab  + urban_lab + sum_diab_treated, data = pred.diab, FUN = mean)
p_standerr_diab <- aggregate(se_diab ~ wealth_quintile_rurb_lab  + urban_lab + sum_diab_treated, data = pred.diab, FUN = mean)
fig4.diab <- left_join(pmeans_diab, p_standerr_diab, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_diab_treated"))
fig4.diab <- mutate(fig4.diab, 
                   lowerci = prob_diab-(1.96*se_diab),
                   upperci = prob_diab+(1.96*se_diab))


write.csv(fig4.diab, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

diabpredfig <- fig4.diab %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_diab, x=wealth_quintile_rurb_lab, color=sum_diab_treated, shape=sum_diab_treated), fun.y="mean", size=3.0, geom="point") +
  #geom_errorbar(aes(ymin=100*lowerci_alt, ymax=100*upperci_alt, x=age_grpOR, color=wealth_quintile_rurb_lab), width=0.3, show.legend = F) +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 7)), name="Number treated Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17,20,24), name="Number treated Comorbidities") 
diabpredfig




```


```{r predicted probabilities diabetes cascade controlled treated vs untreated}


dhs_nomiss_diabetic_only_no5treated <- filter(dhs_nomiss_diabetic_only, sum_diab_treated_dbl<5)


diabetes_controlled <- glm(formula = diabetic_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_diab_treated + wealth_quintile_rurb:sum_diab_treated + wealth_quintile_rurb:urban + sum_diab_treated:urban + d_id , data=dhs_nomiss_diabetic_only_no5treated, family=binomial(link="logit"))



pred.diab.controlled <- filter(dhs_nomiss_diabetic_only_no5treated, is.na(diabetic_controlled_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_diab_treated)==F &  is.na(d_id)==F)
pred.diab <- augment(diabetes_controlled, pred.diab.controlled, type.predict = "response", se.fit = TRUE)

pred.diab <- pred.diab %>% 
  mutate(prob_diab = .fitted,
         se_diab = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_diab <- aggregate(prob_diab ~ wealth_quintile_rurb_lab  + urban_lab + sum_diab_treated, data = pred.diab, FUN = mean)
p_standerr_diab <- aggregate(se_diab ~ wealth_quintile_rurb_lab  + urban_lab + sum_diab_treated, data = pred.diab, FUN = mean)
fig4.diab <- left_join(pmeans_diab, p_standerr_diab, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_diab_treated"))
fig4.diab <- mutate(fig4.diab, 
                   lowerci = prob_diab-(1.96*se_diab),
                   upperci = prob_diab+(1.96*se_diab))


write.csv(fig4.diab, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

diabpredfig <- fig4.diab %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_diab, x=wealth_quintile_rurb_lab, color=sum_diab_treated, shape=sum_diab_treated), fun.y="mean", size=3.0, geom="point") +
  #geom_errorbar(aes(ymin=100*lowerci_alt, ymax=100*upperci_alt, x=age_grpOR, color=wealth_quintile_rurb_lab), width=0.3, show.legend = F) +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 7)), name="Number treated Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17,20,24), name="Number treated Comorbidities") 
diabpredfig




```






```{r regressions hypertension cascade}

##filter
dhs_nomiss_htn_only <- filter(dhs_nomiss, ex_htn_broad_ind==1) 


##htn aware

htn_aware <- glm.cluster(formula = htn_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless +  ex_diab_broad_ind + hiv03 + heart + cancer + thyroid + anemia + asthma + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=binomial(link="logit"))

aware <- exp(cbind(OR = coef(htn_aware), confint(htn_aware)))
write.csv(aware, "OR htn aware .csv")

results_aware <-summary(htn_aware)
write.csv(results_aware, "p Value htn aware.csv")

##htn treated

htn_treated <- glm.cluster(formula = htn_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + ex_diab_broad_ind + hiv03 + heart + cancer + thyroid + anemia + asthma + d_id,  cluster="psu", data=dhs_nomiss_htn_only, family=binomial(link="logit"))

treated <- exp(cbind(OR = coef(htn_treated), confint(htn_treated)))
write.csv(treated, "OR htn treated .csv")

results_treated <-summary(htn_treated)
write.csv(results_treated, "p Value htn treated.csv")

##htn controlled

htn_controlled <- glm.cluster(formula = htn_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + sex + bmi_group + tobacco_smoked + tobacco_smokeless + ex_htn_broad_ind + hiv03 + heart + cancer + thyroid + anemia + asthma + d_id ,  cluster="psu", data=dhs_nomiss_htn_only, family=binomial(link="logit"))

controlled <- exp(cbind(OR = coef(htn_controlled), confint(htn_controlled)))
write.csv(controlled, "OR htn controlled .csv")

results_controlled <-summary(htn_controlled)
write.csv(results_controlled, "p Value htn controlled.csv")



```








`


```{r predicted probabilities htn cascade screened}


dhs_nomiss_htn_only_no4comorb <- filter(dhs_nomiss_htn_only, sum_multihtn_dbl<4)



htn_screened <- glm(formula = htn_screened_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_multihtn + wealth_quintile_rurb:sum_multihtn + wealth_quintile_rurb:urban + sum_multihtn:urban + d_id , data=dhs_nomiss_htn_only_no4comorb, family=binomial(link="logit"))



pred.htn.screened <- filter(dhs_nomiss_htn_only_no4comorb, is.na(htn_screened_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_multihtn)==F &  is.na(d_id)==F)
pred.htn <- augment(htn_screened, pred.htn.screened, type.predict = "response", se.fit = TRUE)

pred.htn <- pred.htn %>% 
  mutate(prob_htn = .fitted,
         se_htn = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_htn <- aggregate(prob_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_multihtn, data = pred.htn, FUN = mean)
p_standerr_htn <- aggregate(se_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_multihtn, data = pred.htn, FUN = mean)
fig4.htn <- left_join(pmeans_htn, p_standerr_htn, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_multihtn"))
fig4.htn <- mutate(fig4.htn, 
                   lowerci = prob_htn-(1.96*se_htn),
                   upperci = prob_htn+(1.96*se_htn))


write.csv(fig4.htn, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

htnpredfig <- fig4.htn %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_htn, x=wealth_quintile_rurb_lab, color=sum_multihtn, shape=sum_multihtn), fun.y="mean", size=3.0, geom="point") +
  #geom_errorbar(aes(ymin=100*lowerci_alt, ymax=100*upperci_alt, x=age_grpOR, color=wealth_quintile_rurb_lab), width=0.3, show.legend = F) +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 4)), name="Number Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17,20,24), name="Number Comorbidities") 
htnpredfig




```




```{r predicted probabilities htn cascade aware}


dhs_nomiss_htn_only_no4comorb <- filter(dhs_nomiss_htn_only, sum_multihtn_dbl<4)



htn_aware <- glm(formula = htn_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_multihtn + wealth_quintile_rurb:sum_multihtn + wealth_quintile_rurb:urban + sum_multihtn:urban + d_id , data=dhs_nomiss_htn_only_no4comorb, family=binomial(link="logit"))



pred.htn.aware <- filter(dhs_nomiss_htn_only_no4comorb, is.na(htn_aware_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_multihtn)==F &  is.na(d_id)==F)
pred.htn <- augment(htn_aware, pred.htn.aware, type.predict = "response", se.fit = TRUE)

pred.htn <- pred.htn %>% 
  mutate(prob_htn = .fitted,
         se_htn = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_htn <- aggregate(prob_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_multihtn, data = pred.htn, FUN = mean)
p_standerr_htn <- aggregate(se_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_multihtn, data = pred.htn, FUN = mean)
fig4.htn <- left_join(pmeans_htn, p_standerr_htn, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_multihtn"))
fig4.htn <- mutate(fig4.htn, 
                   lowerci = prob_htn-(1.96*se_htn),
                   upperci = prob_htn+(1.96*se_htn))


write.csv(fig4.htn, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

htnpredfig <- fig4.htn %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_htn, x=wealth_quintile_rurb_lab, color=sum_multihtn, shape=sum_multihtn), fun.y="mean", size=3.0, geom="point") +
  #geom_errorbar(aes(ymin=100*lowerci_alt, ymax=100*upperci_alt, x=age_grpOR, color=wealth_quintile_rurb_lab), width=0.3, show.legend = F) +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 4)), name="Number Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17,20,24), name="Number Comorbidities") 
htnpredfig




```




```{r predicted probabilities htn cascade treated}


dhs_nomiss_htn_only_no4comorb <- filter(dhs_nomiss_htn_only, sum_multihtn_dbl<4)


htn_treated <- glm(formula = htn_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_multihtn + wealth_quintile_rurb:sum_multihtn + wealth_quintile_rurb:urban + sum_multihtn:urban + d_id , data=dhs_nomiss_htn_only_no4comorb, family=binomial(link="logit"))



pred.htn.treated <- filter(dhs_nomiss_htn_only_no4comorb, is.na(htn_treated_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_multihtn)==F &  is.na(d_id)==F)
pred.htn <- augment(htn_treated, pred.htn.treated, type.predict = "response", se.fit = TRUE)

pred.htn <- pred.htn %>% 
  mutate(prob_htn = .fitted,
         se_htn = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_htn <- aggregate(prob_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_multihtn, data = pred.htn, FUN = mean)
p_standerr_htn <- aggregate(se_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_multihtn, data = pred.htn, FUN = mean)
fig4.htn <- left_join(pmeans_htn, p_standerr_htn, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_multihtn"))
fig4.htn <- mutate(fig4.htn, 
                   lowerci = prob_htn-(1.96*se_htn),
                   upperci = prob_htn+(1.96*se_htn))


write.csv(fig4.htn, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

htnpredfig <- fig4.htn %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_htn, x=wealth_quintile_rurb_lab, color=sum_multihtn, shape=sum_multihtn), fun.y="mean", size=3.0, geom="point") +
  #geom_errorbar(aes(ymin=100*lowerci_alt, ymax=100*upperci_alt, x=age_grpOR, color=wealth_quintile_rurb_lab), width=0.3, show.legend = F) +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 4)), name="Number Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17,20,24), name="Number Comorbidities") 
htnpredfig




```


```{r predicted probabilities htn cascade controlled}


dhs_nomiss_htn_only_no4comorb <- filter(dhs_nomiss_htn_only, sum_multihtn_dbl<4)



htn_controlled <- glm(formula = htn_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_multihtn + wealth_quintile_rurb:sum_multihtn + wealth_quintile_rurb:urban + sum_multihtn:urban + d_id , data=dhs_nomiss_htn_only_no4comorb, family=binomial(link="logit"))



pred.htn.controlled <- filter(dhs_nomiss_htn_only_no4comorb, is.na(htn_controlled_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_multihtn)==F &  is.na(d_id)==F)
pred.htn <- augment(htn_controlled, pred.htn.controlled, type.predict = "response", se.fit = TRUE)

pred.htn <- pred.htn %>% 
  mutate(prob_htn = .fitted,
         se_htn = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_htn <- aggregate(prob_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_multihtn, data = pred.htn, FUN = mean)
p_standerr_htn <- aggregate(se_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_multihtn, data = pred.htn, FUN = mean)
fig4.htn <- left_join(pmeans_htn, p_standerr_htn, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_multihtn"))
fig4.htn <- mutate(fig4.htn, 
                   lowerci = prob_htn-(1.96*se_htn),
                   upperci = prob_htn+(1.96*se_htn))


write.csv(fig4.htn, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

htnpredfig <- fig4.htn %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_htn, x=wealth_quintile_rurb_lab, color=sum_multihtn, shape=sum_multihtn), fun.y="mean", size=3.0, geom="point") +
  #geom_errorbar(aes(ymin=100*lowerci_alt, ymax=100*upperci_alt, x=age_grpOR, color=wealth_quintile_rurb_lab), width=0.3, show.legend = F) +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 4)), name="Number Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17,20,24), name="Number Comorbidities") 
htnpredfig




```




```{r predicted probabilities htn cascade screened treated vs untreated}


dhs_nomiss_htn_only_no5treated <- filter(dhs_nomiss_htn_only, sum_htn_treated_dbl<5)




htn_screened <- glm(formula = htn_screened_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_htn_treated + wealth_quintile_rurb:sum_multihtn + wealth_quintile_rurb:urban + sum_multihtn:urban + d_id , data=dhs_nomiss_htn_only_no5treated, family=binomial(link="logit"))



pred.htn.screened <- filter(dhs_nomiss_htn_only_no5treated, is.na(htn_screened_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_htn_treated)==F &  is.na(d_id)==F)
pred.htn <- augment(htn_screened, pred.htn.screened, type.predict = "response", se.fit = TRUE)

pred.htn <- pred.htn %>% 
  mutate(prob_htn = .fitted,
         se_htn = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_htn <- aggregate(prob_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_htn_treated, data = pred.htn, FUN = mean)
p_standerr_htn <- aggregate(se_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_htn_treated, data = pred.htn, FUN = mean)
fig4.htn <- left_join(pmeans_htn, p_standerr_htn, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_htn_treated"))
fig4.htn <- mutate(fig4.htn, 
                   lowerci = prob_htn-(1.96*se_htn),
                   upperci = prob_htn+(1.96*se_htn))


write.csv(fig4.htn, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

htnpredfig <- fig4.htn %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_htn, x=wealth_quintile_rurb_lab, color=sum_htn_treated, shape=sum_htn_treated), fun.y="mean", size=3.0, geom="point") +
  #geom_errorbar(aes(ymin=100*lowerci_alt, ymax=100*upperci_alt, x=age_grpOR, color=wealth_quintile_rurb_lab), width=0.3, show.legend = F) +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 7)), name="Number treated Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17,20,24), name="Number treated Comorbidities") 
htnpredfig




```




```{r predicted probabilities htn cascade aware treated vs untreated}


dhs_nomiss_htn_only_no5treated <- filter(dhs_nomiss_htn_only, sum_htn_treated_dbl<5)




htn_aware <- glm(formula = htn_aware_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_htn_treated + wealth_quintile_rurb:sum_htn_treated + wealth_quintile_rurb:urban + sum_htn_treated:urban + d_id , data=dhs_nomiss_htn_only_no5treated, family=binomial(link="logit"))



pred.htn.aware <- filter(dhs_nomiss_htn_only_no5treated, is.na(htn_aware_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_htn_treated)==F &  is.na(d_id)==F)
pred.htn <- augment(htn_aware, pred.htn.aware, type.predict = "response", se.fit = TRUE)

pred.htn <- pred.htn %>% 
  mutate(prob_htn = .fitted,
         se_htn = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_htn <- aggregate(prob_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_htn_treated, data = pred.htn, FUN = mean)
p_standerr_htn <- aggregate(se_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_htn_treated, data = pred.htn, FUN = mean)
fig4.htn <- left_join(pmeans_htn, p_standerr_htn, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_htn_treated"))
fig4.htn <- mutate(fig4.htn, 
                   lowerci = prob_htn-(1.96*se_htn),
                   upperci = prob_htn+(1.96*se_htn))


write.csv(fig4.htn, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

htnpredfig <- fig4.htn %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_htn, x=wealth_quintile_rurb_lab, color=sum_htn_treated, shape=sum_htn_treated), fun.y="mean", size=3.0, geom="point") +
  #geom_errorbar(aes(ymin=100*lowerci_alt, ymax=100*upperci_alt, x=age_grpOR, color=wealth_quintile_rurb_lab), width=0.3, show.legend = F) +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 7)), name="Number treated Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17,20,24), name="Number treated Comorbidities") 
htnpredfig




```




```{r predicted probabilities htn cascade treated treated vs untreated}


dhs_nomiss_htn_only_no5treated <- filter(dhs_nomiss_htn_only, sum_htn_treated_dbl<5)

htn_treated <- glm(formula = htn_treated_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_htn_treated + wealth_quintile_rurb:sum_htn_treated + wealth_quintile_rurb:urban + sum_htn_treated:urban + d_id , data=dhs_nomiss_htn_only_no5treated, family=binomial(link="logit"))



pred.htn.treated <- filter(dhs_nomiss_htn_only_no5treated, is.na(htn_treated_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_htn_treated)==F &  is.na(d_id)==F)
pred.htn <- augment(htn_treated, pred.htn.treated, type.predict = "response", se.fit = TRUE)

pred.htn <- pred.htn %>% 
  mutate(prob_htn = .fitted,
         se_htn = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_htn <- aggregate(prob_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_htn_treated, data = pred.htn, FUN = mean)
p_standerr_htn <- aggregate(se_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_htn_treated, data = pred.htn, FUN = mean)
fig4.htn <- left_join(pmeans_htn, p_standerr_htn, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_htn_treated"))
fig4.htn <- mutate(fig4.htn, 
                   lowerci = prob_htn-(1.96*se_htn),
                   upperci = prob_htn+(1.96*se_htn))


write.csv(fig4.htn, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

htnpredfig <- fig4.htn %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_htn, x=wealth_quintile_rurb_lab, color=sum_htn_treated, shape=sum_htn_treated), fun.y="mean", size=3.0, geom="point") +
  #geom_errorbar(aes(ymin=100*lowerci_alt, ymax=100*upperci_alt, x=age_grpOR, color=wealth_quintile_rurb_lab), width=0.3, show.legend = F) +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 7)), name="Number treated Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17,20,24), name="Number treated Comorbidities") 
htnpredfig




```


```{r predicted probabilities htn cascade controlled treated vs untreated}


dhs_nomiss_htn_only_no5treated <- filter(dhs_nomiss_htn_only, sum_htn_treated_dbl<5)


htn_controlled <- glm(formula = htn_controlled_dbl ~ age_grpOR + wealth_quintile_rurb + educatnames + married + urban + bmi_group + tobacco_smoked + tobacco_smokeless + sum_htn_treated + wealth_quintile_rurb:sum_htn_treated + wealth_quintile_rurb:urban + sum_htn_treated:urban + d_id , data=dhs_nomiss_htn_only_no5treated, family=binomial(link="logit"))



pred.htn.controlled <- filter(dhs_nomiss_htn_only_no5treated, is.na(htn_controlled_dbl)==FALSE  & is.na(age_grpOR)==FALSE & is.na(wealth_quintile_rurb)==FALSE & is.na(educatnames)==FALSE &  is.na(married)==FALSE & is.na(urban)==FALSE & is.na(bmi_group)==FALSE & is.na(tobacco_smoked)==F & is.na(tobacco_smokeless)==F & is.na(sum_htn_treated)==F &  is.na(d_id)==F)
pred.htn <- augment(htn_controlled, pred.htn.controlled, type.predict = "response", se.fit = TRUE)

pred.htn <- pred.htn %>% 
  mutate(prob_htn = .fitted,
         se_htn = .se.fit) %>% 
  dplyr::select(-.resid, -.hat, -.sigma, -.cooksd, -.std.resid, -.fitted, -.se.fit)


# Create dataset for plotting
pmeans_htn <- aggregate(prob_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_htn_treated, data = pred.htn, FUN = mean)
p_standerr_htn <- aggregate(se_htn ~ wealth_quintile_rurb_lab  + urban_lab + sum_htn_treated, data = pred.htn, FUN = mean)
fig4.htn <- left_join(pmeans_htn, p_standerr_htn, by = c( "wealth_quintile_rurb_lab", "urban_lab", "sum_htn_treated"))
fig4.htn <- mutate(fig4.htn, 
                   lowerci = prob_htn-(1.96*se_htn),
                   upperci = prob_htn+(1.96*se_htn))


write.csv(fig4.htn, "regressionfigure_pred prob number comorbidities.csv")


brightness <- function(rgbcol, v) {
  conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
  conv[[3]] <- v
  do.call(hsv, conv)
}

htnpredfig <- fig4.htn %>% 
  ggplot() +
  stat_summary(aes(y=100*prob_htn, x=wealth_quintile_rurb_lab, color=sum_htn_treated, shape=sum_htn_treated), fun.y="mean", size=3.0, geom="point") +
  #geom_errorbar(aes(ymin=100*lowerci_alt, ymax=100*upperci_alt, x=age_grpOR, color=wealth_quintile_rurb_lab), width=0.3, show.legend = F) +
  facet_wrap(~urban_lab) +
  coord_fixed(ylim=c(0, 100), ratio= 5/100, expand=T) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Probability, %",
       fill="") +
  theme(axis.text.y=element_text(size=18, family="Times"),
        axis.text.x=element_text(size=18, angle=45, hjust=1, family="Times"),
        axis.title=element_text(size=21, face="bold", family="Times"),
        legend.title=element_text(size=18, family="Times"),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times"), 
        panel.spacing = unit(2.5, "lines"),
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        plot.title = element_text(size=24, face="bold", family="Times"))+
  scale_colour_manual(values = brightness("red", seq(0.0, 1.0, length = 7)), name="Number treated Comorbidities") +
  scale_shape_manual(values = c(19, 25, 22, 23, 17,20,24), name="Number treated Comorbidities") 
htnpredfig




```



```{r capital figure##################}

####CAPITAL FIGURE WITH ERRORBARS###########################################

###check p-value
summary(lm(formula = state_PCI ~ htn_aware, data = prevtotaware))
summary(lm(formula = state_PCI ~ htn_treated, data = prevtottreated))
summary(lm(formula = state_PCI ~ htn_controlled, data = prevtotcontrolled))



#####Aware

dhs_nomiss_noNAinpsu <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)

###create PSU ID in dhs_nomiss_diabetic_threecomorb :
dhs_nomiss_advice_three <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_advice_three$psuid <- as.factor(dhs_nomiss_advice_three$psuid)

###make noNAinpsu

dhs_nomiss_advice_three_noNAinpsu <- filter(dhs_nomiss_advice_three, is.na(psu)==F)

summary(dhs_nomiss_advice_three_noNAinpsu$psu)

sum(is.na(dhs_nomiss_advice_three_noNAinpsu$psu)==T)



svy_aware <- dhs_nomiss_advice_three_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_aware_dbl, ex_state_ind))

prevtotaware <- svy_aware %>%
  group_by(ex_state_ind) %>% 
  summarize(htn_aware_prop = survey_mean(both_aware_dbl, proportion=TRUE, vartype = "ci")) 
 
prevtotaware <- mutate(prevtotaware, 
                  htn_aware_prop = htn_aware_prop*100,
                  htn_aware_prop_low= htn_aware_prop_low*100,
                  htn_aware_prop_upp= htn_aware_prop_upp*100)
                  



write.csv(prevtotaware, "combined cascade aware.csv")

################## Zones as per: https://en.wikipedia.org/wiki/Administrative_divisions_of_India
prevtotaware <- mutate(prevtotaware, 
                  # Nothern
                  zone = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan" | ex_state_ind=="Jammu and Kashmir", "North",
                                          # Northeastern
                                          ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                 # Central
                                                 ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh" | ex_state_ind=="Uttarakhand" | ex_state_ind== "Uttar Pradesh", "Central",
                                                        # Eastern
                                                        ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                               # Western
                                                               ifelse(ex_state_ind=="Daman and Diu" | ex_state_ind=="Goa" | ex_state_ind=="Maharashtra" | ex_state_ind=="Gujarat", "West",
                                                                      # Southern
                                                                      ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA))))))))





####### Adding state labels
prevtotaware <- mutate(prevtotaware, state_lab = fct_recode(ex_state_ind, 
                                                  "HP" = "Himachal Pradesh",
                                                  "PB" = "Punjab",
                                                  "CH" = "Chandigarh",
                                                  "HR" = "Haryana",
                                                  "DL" = "Delhi",
                                                  "SK" = "Sikkim",
                                                  "DD" = "Daman and Diu",
                                                  "AR" = "Arunachal Pradesh",
                                                  "NL" = "Nagaland",
                                                  "MN" = "Manipur",
                                                  "MZ" = "Mizoram",
                                                  "TR" = "Tripura",
                                                  "ML" = "Meghalaya",
                                                  "WB" = "West Bengal",
                                                  "MH" = "Maharashtra",
                                                  "AP" = "Andhra Pradesh",
                                                  "KA" = "Karnataka",
                                                  "GA" = "Goa",
                                                  "KL" = "Kerala",
                                                  "PY" = "Puducherry",
                                                  "TN" = "Tamil Nadu",
                                                  "AN" = "Andaman and Nicobar Islands",
                                                  "TS" = "Telangana",
                                                  "UK" = "Uttarakhand",
                                                  "RJ" = "Rajasthan",
                                                  "UP" = "Uttar Pradesh",
                                                  "BR" = "Bihar",
                                                  "AS" = "Assam",
                                                  "JK" = "Jammu and Kashmir",
                                                  "GJ" = "Gujarat",
                                                  "JH" = "Jharkhand",
                                                  "OD" = "Odisha",
                                                  "CT" = "Chhattisgarh", 
                                                  "MP" = "Madhya Pradesh"))



prevtotaware <- mutate(prevtotaware, 
                  urban_dbl = as.numeric(urban))

#install.packages("spatstat")


prevtotaware <- mutate(prevtotaware,
                  state_PCI = ifelse(ex_state_ind== "Andaman and Nicobar Islands",161595.035*(1/16.734), 
                                     ifelse(ex_state_ind== "Andhra Pradesh",93989.48897*(1/16.734),
                                            ifelse(ex_state_ind== "Arunachal Pradesh",97887.8059*(1/16.734),
                                                   ifelse(ex_state_ind== "Assam",51099.8419*(1/16.734),
                                                          ifelse(ex_state_ind== "Bihar",33012.94996*(1/16.734),
                                                                 ifelse(ex_state_ind== "Chandigarh",275484.3905*(1/16.734),
                                                                        ifelse(ex_state_ind== "Chhattisgarh",72687.6339*(1/16.734),
                                                                               ifelse(ex_state_ind== "Delhi",232979.7323*(1/16.734),
                                                                                      ifelse(ex_state_ind== "Goa",335245.0559*(1/16.734),
                                                                                             ifelse(ex_state_ind== "Gujarat",126678.0115*(1/16.734),
                                                                                                    ifelse(ex_state_ind== "Haryana",153410.0874*(1/16.734),
                                                                                                           ifelse(ex_state_ind== "Himachal Pradesh",120305.5909*(1/16.734),
                                                                                                                  ifelse(ex_state_ind== "Jammu and Kashmir",69825.28608*(1/16.734),
                                                                                                                         ifelse(ex_state_ind== "Jharkhand",52374.28707*(1/16.734),
                                                                                                                                ifelse(ex_state_ind== "Karnataka",100598.087*(1/16.734),
                                                                                                                                       ifelse(ex_state_ind== "Kerala",118625.7787*(1/16.734),
                                                                                                                                              ifelse(ex_state_ind== "Madhya Pradesh",59858.0615*(1/16.734),
                                                                                                                                                     ifelse(ex_state_ind== "Maharashtra",134384.0679*(1/16.734),
                                                                                                                                                            ifelse(ex_state_ind== "Manipur",50157.67944*(1/16.734),
                                                                                                                                                                   ifelse(ex_state_ind== "Meghalaya",73888.84451*(1/16.734),
                                                                                                                                                                          ifelse(ex_state_ind== "Mizoram",93847.46347*(1/16.734),
                                                                                                                                                                                 ifelse(ex_state_ind== "Nagaland",89709.28511*(1/16.734),
                                                                                                                                                                                        ifelse(ex_state_ind== "Odisha",65035.16039*(1/16.734),
                                                                                                                                                                                               ifelse(ex_state_ind== "Puducherry",168892.5785*(1/16.734),
                                                                                                                                                                                                      ifelse(ex_state_ind== "Punjab",114462.0737*(1/16.734),
                                                                                                                                                                                                             ifelse(ex_state_ind== "Rajasthan",75510.83915*(1/16.734),
                                                                                                                                                                                                                    ifelse(ex_state_ind== "Sikkim",202709.8957*(1/16.734),
                                                                                                                                                                                                                           ifelse(ex_state_ind== "Tamil Nadu",118402.3791*(1/16.734),
                                                                                                                                                                                                                                  ifelse(ex_state_ind== "Tripura",72973.88591*(1/16.734),
                                                                                                                                                                                                                                         ifelse(ex_state_ind== "Uttar Pradesh",43177.81353*(1/16.734),
                                                                                                                                                                                                                                                ifelse(ex_state_ind== "Uttarakhand",121845.5702*(1/16.734),
                                                                                                                                                                                                                                                       ifelse(ex_state_ind== "West Bengal",77409.18859*(1/16.734),
                                                                                                                                                                                                                                                              ifelse(ex_state_ind== "Telangana",111311.9409*(1/16.734),NA))))))))))))))))))))))))))))))))))







prevtotaware <- dplyr::filter(prevtotaware, ex_state_ind != "Puducherry") 
prevtotaware <- dplyr::filter(prevtotaware, ex_state_ind != "Daman and Diu")
prevtotaware <- dplyr::filter(prevtotaware, ex_state_ind != "Delhi")
prevtotaware <- dplyr::filter(prevtotaware, ex_state_ind != "Dadra and Nagar Haveli")
prevtotaware <- dplyr::filter(prevtotaware, ex_state_ind != "Andaman and Nicobar Islands")
prevtotaware <- dplyr::filter(prevtotaware, ex_state_ind != "Lakshadweep")
prevtotaware <- dplyr::filter(prevtotaware, ex_state_ind != "Chandigarh")


write.csv(prevtotaware, "combined cascade aware GDP figure.csv")

#####aware 

awarebygdp <- prevtotaware %>% 
  ggplot() +
  geom_smooth(aes(y=htn_aware, x=state_PCI), color="grey50", fill="grey80", alpha=0.7, 
              se=T, method="lm", size=0.7, fullrange=T) +
  geom_point(aes(y= htn_aware, x=state_PCI, color=as.factor(zone)), size=3) +
  geom_text_repel(mapping=aes(y=htn_aware, x=state_PCI, 
                              label = state_lab, fontface=2), 
                  size = 5, segment.color = "black", segment.size = 0.3, family="Times") +
  geom_errorbar(aes(ymin=htn_low, ymax=htn_upp, x=state_PCI, color=as.factor(zone)), 
                width=0.05, alpha=0.5, show.legend = F) +
  theme_classic() + 
  labs(x = "GDP per capita (int. $)",
       y = "Percent",
       fill="") + 
  theme(axis.text.y=element_text(size=20),
        axis.text.x=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=15, family="Times"),
        legend.title = element_blank(),
        #legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines"),
        title = element_text(size=24, face="bold", family="Times")) + 
  scale_color_brewer(palette="Set1") +
  scale_y_continuous(breaks = seq(from=0, to=100, by=20), limits=c(0, 140)) +
  scale_x_continuous(breaks = seq(from=0, to=25000, by=5000), limits=c(0, 25000)) +
  coord_fixed(25000/100, expand=F)
awarebygdp 


```


```{r capital figure treated}

#####treated


dhs_nomiss_noNAinpsu <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)

###create PSU ID in dhs_nomiss_diabetic_threecomorb :
dhs_nomiss_advice_three <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_advice_three$psuid <- as.factor(dhs_nomiss_advice_three$psuid)

###make noNAinpsu

dhs_nomiss_advice_three_noNAinpsu <- filter(dhs_nomiss_advice_three, is.na(psu)==F)

summary(dhs_nomiss_advice_three_noNAinpsu$psu)

sum(is.na(dhs_nomiss_advice_three_noNAinpsu$psu)==T)



svy_treated <- dhs_nomiss_advice_three_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_treated_dbl, ex_state_ind))

prevtottreated <- svy_treated %>%
  group_by(ex_state_ind) %>% 
  summarize(htn_treated_prop = survey_mean(both_treated_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(htn_treated = 100*htn_treated_prop,
         htn_low = 100*htn_treated_prop_low,
         htn_upp = 100*htn_treated_prop_upp)


################## Zones as per: https://en.wikipedia.org/wiki/Administrative_divisions_of_India
prevtottreated <- mutate(prevtottreated, 
                       # Nothern
                       zone = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan" | ex_state_ind=="Jammu and Kashmir", "North",
                                               # Northeastern
                                               ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                      # Central
                                                      ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh" | ex_state_ind=="Uttarakhand" | ex_state_ind== "Uttar Pradesh", "Central",
                                                             # Eastern
                                                             ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                                    # Western
                                                                    ifelse(ex_state_ind=="Daman and Diu" | ex_state_ind=="Goa" | ex_state_ind=="Maharashtra" | ex_state_ind=="Gujarat", "West",
                                                                           # Southern
                                                                           ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA))))))))





####### Adding state labels
prevtottreated <- mutate(prevtottreated, state_lab = fct_recode(ex_state_ind, 
                                                            "HP" = "Himachal Pradesh",
                                                            "PB" = "Punjab",
                                                            "CH" = "Chandigarh",
                                                            "HR" = "Haryana",
                                                            "DL" = "Delhi",
                                                            "SK" = "Sikkim",
                                                            "DD" = "Daman and Diu",
                                                            "AR" = "Arunachal Pradesh",
                                                            "NL" = "Nagaland",
                                                            "MN" = "Manipur",
                                                            "MZ" = "Mizoram",
                                                            "TR" = "Tripura",
                                                            "ML" = "Meghalaya",
                                                            "WB" = "West Bengal",
                                                            "MH" = "Maharashtra",
                                                            "AP" = "Andhra Pradesh",
                                                            "KA" = "Karnataka",
                                                            "GA" = "Goa",
                                                            "KL" = "Kerala",
                                                            "PY" = "Puducherry",
                                                            "TN" = "Tamil Nadu",
                                                            "AN" = "Andaman and Nicobar Islands",
                                                            "TS" = "Telangana",
                                                            "UK" = "Uttarakhand",
                                                            "RJ" = "Rajasthan",
                                                            "UP" = "Uttar Pradesh",
                                                            "BR" = "Bihar",
                                                            "AS" = "Assam",
                                                            "JK" = "Jammu and Kashmir",
                                                            "GJ" = "Gujarat",
                                                            "JH" = "Jharkhand",
                                                            "OD" = "Odisha",
                                                            "CT" = "Chhattisgarh", 
                                                            "MP" = "Madhya Pradesh"))



prevtottreated <- mutate(prevtottreated, 
                       urban_dbl = as.numeric(urban))

#install.packages("spatstat")


prevtottreated <- mutate(prevtottreated,
                       state_PCI = ifelse(ex_state_ind== "Andaman and Nicobar Islands",161595.035*(1/16.734), 
                                          ifelse(ex_state_ind== "Andhra Pradesh",93989.48897*(1/16.734),
                                                 ifelse(ex_state_ind== "Arunachal Pradesh",97887.8059*(1/16.734),
                                                        ifelse(ex_state_ind== "Assam",51099.8419*(1/16.734),
                                                               ifelse(ex_state_ind== "Bihar",33012.94996*(1/16.734),
                                                                      ifelse(ex_state_ind== "Chandigarh",275484.3905*(1/16.734),
                                                                             ifelse(ex_state_ind== "Chhattisgarh",72687.6339*(1/16.734),
                                                                                    ifelse(ex_state_ind== "Delhi",232979.7323*(1/16.734),
                                                                                           ifelse(ex_state_ind== "Goa",335245.0559*(1/16.734),
                                                                                                  ifelse(ex_state_ind== "Gujarat",126678.0115*(1/16.734),
                                                                                                         ifelse(ex_state_ind== "Haryana",153410.0874*(1/16.734),
                                                                                                                ifelse(ex_state_ind== "Himachal Pradesh",120305.5909*(1/16.734),
                                                                                                                       ifelse(ex_state_ind== "Jammu and Kashmir",69825.28608*(1/16.734),
                                                                                                                              ifelse(ex_state_ind== "Jharkhand",52374.28707*(1/16.734),
                                                                                                                                     ifelse(ex_state_ind== "Karnataka",100598.087*(1/16.734),
                                                                                                                                            ifelse(ex_state_ind== "Kerala",118625.7787*(1/16.734),
                                                                                                                                                   ifelse(ex_state_ind== "Madhya Pradesh",59858.0615*(1/16.734),
                                                                                                                                                          ifelse(ex_state_ind== "Maharashtra",134384.0679*(1/16.734),
                                                                                                                                                                 ifelse(ex_state_ind== "Manipur",50157.67944*(1/16.734),
                                                                                                                                                                        ifelse(ex_state_ind== "Meghalaya",73888.84451*(1/16.734),
                                                                                                                                                                               ifelse(ex_state_ind== "Mizoram",93847.46347*(1/16.734),
                                                                                                                                                                                      ifelse(ex_state_ind== "Nagaland",89709.28511*(1/16.734),
                                                                                                                                                                                             ifelse(ex_state_ind== "Odisha",65035.16039*(1/16.734),
                                                                                                                                                                                                    ifelse(ex_state_ind== "Puducherry",168892.5785*(1/16.734),
                                                                                                                                                                                                           ifelse(ex_state_ind== "Punjab",114462.0737*(1/16.734),
                                                                                                                                                                                                                  ifelse(ex_state_ind== "Rajasthan",75510.83915*(1/16.734),
                                                                                                                                                                                                                         ifelse(ex_state_ind== "Sikkim",202709.8957*(1/16.734),
                                                                                                                                                                                                                                ifelse(ex_state_ind== "Tamil Nadu",118402.3791*(1/16.734),
                                                                                                                                                                                                                                       ifelse(ex_state_ind== "Tripura",72973.88591*(1/16.734),
                                                                                                                                                                                                                                              ifelse(ex_state_ind== "Uttar Pradesh",43177.81353*(1/16.734),
                                                                                                                                                                                                                                                     ifelse(ex_state_ind== "Uttarakhand",121845.5702*(1/16.734),
                                                                                                                                                                                                                                                            ifelse(ex_state_ind== "West Bengal",77409.18859*(1/16.734),
                                                                                                                                                                                                                                                                   ifelse(ex_state_ind== "Telangana",111311.9409*(1/16.734),NA))))))))))))))))))))))))))))))))))







prevtottreated <- dplyr::filter(prevtottreated, ex_state_ind != "Puducherry") 
prevtottreated <- dplyr::filter(prevtottreated, ex_state_ind != "Daman and Diu")
prevtottreated <- dplyr::filter(prevtottreated, ex_state_ind != "Delhi")
prevtottreated <- dplyr::filter(prevtottreated, ex_state_ind != "Dadra and Nagar Haveli")
prevtottreated <- dplyr::filter(prevtottreated, ex_state_ind != "Andaman and Nicobar Islands")
prevtottreated <- dplyr::filter(prevtottreated, ex_state_ind != "Lakshadweep")
prevtottreated <- dplyr::filter(prevtottreated, ex_state_ind != "Chandigarh")


write.csv(prevtottreated, "combined cascade treated gdp per capita.csv")
#####treated 

treatedbygdp <- prevtottreated %>% 
  ggplot() +
  geom_smooth(aes(y=htn_treated, x=state_PCI), color="grey50", fill="grey80", alpha=0.7, 
              se=T, method="lm", size=0.7, fullrange=T) +
  geom_point(aes(y= htn_treated, x=state_PCI, color=as.factor(zone)), size=3) +
  geom_text_repel(mapping=aes(y=htn_treated, x=state_PCI, 
                              label = state_lab, fontface=2), 
                  size = 5, segment.color = "black", segment.size = 0.3, family="Times") +
  geom_errorbar(aes(ymin=htn_low, ymax=htn_upp, x=state_PCI, color=as.factor(zone)), 
                width=0.05, alpha=0.5, show.legend = F) +
  theme_classic() + 
  labs(x = "GDP per capita (int. $)",
       y = "Percent",
       fill="") + 
  theme(axis.text.y=element_text(size=20),
        axis.text.x=element_text(size=20),
        axis.title=element_text(size=22, face="bold"),
        legend.text=element_text(size=15, family="Times"),
        legend.title = element_blank(),
        #legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22, face="bold"),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines"),
        title = element_text(size=24, face="bold", family="Times")) + 
  scale_color_brewer(palette="Set1") +
  scale_y_continuous(breaks = seq(from=0, to=100, by=20), limits=c(0, 140)) +
  scale_x_continuous(breaks = seq(from=0, to=25000, by=5000), limits=c(0, 25000)) +
  coord_fixed(25000/100, expand=F)
treatedbygdp 

```


```{r capital figure controlled}


#####controlled



dhs_nomiss_noNAinpsu <- filter(dhs_nomiss, ex_diab_broad_ind==1 & ex_htn_broad_ind==1)

###create PSU ID in dhs_nomiss_diabetic_threecomorb :
dhs_nomiss_advice_three <- dhs_nomiss_noNAinpsu %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_advice_three$psuid <- as.factor(dhs_nomiss_advice_three$psuid)

###make noNAinpsu

dhs_nomiss_advice_three_noNAinpsu <- filter(dhs_nomiss_advice_three, is.na(psu)==F)

summary(dhs_nomiss_advice_three_noNAinpsu$psu)

sum(is.na(dhs_nomiss_advice_three_noNAinpsu$psu)==T)



svy_controlled <- dhs_nomiss_advice_three_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(both_controlled_dbl, ex_state_ind))

prevtotcontrolled <- svy_controlled %>%
  group_by(ex_state_ind) %>% 
  summarize(htn_controlled_prop = survey_mean(both_controlled_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(htn_controlled = 100*htn_controlled_prop,
         htn_low = 100*htn_controlled_prop_low,
         htn_upp = 100*htn_controlled_prop_upp)


################## Zones as per: https://en.wikipedia.org/wiki/Administrative_divisions_of_India
prevtotcontrolled <- mutate(prevtotcontrolled, 
                         # Nothern
                         zone = as.factor(ifelse(ex_state_ind=="Chandigarh" | ex_state_ind=="NCT of Delhi" | ex_state_ind=="Haryana" | ex_state_ind=="Himachal Pradesh" | ex_state_ind=="Punjab" | ex_state_ind=="Rajasthan" | ex_state_ind=="Jammu and Kashmir", "North",
                                                 # Northeastern
                                                 ifelse(ex_state_ind=="Assam" | ex_state_ind=="Arunachal Pradesh" | ex_state_ind=="Manipur" | ex_state_ind=="Meghalaya" | ex_state_ind=="Mizoram" | ex_state_ind=="Nagaland" | ex_state_ind=="Sikkim" | ex_state_ind=="Tripura", "Northeast",
                                                        # Central
                                                        ifelse(ex_state_ind=="Chhattisgarh" | ex_state_ind=="Madhya Pradesh" | ex_state_ind=="Uttarakhand" | ex_state_ind== "Uttar Pradesh", "Central",
                                                               # Eastern
                                                               ifelse(ex_state_ind=="Bihar" | ex_state_ind=="Jharkhand" | ex_state_ind=="Odisha" | ex_state_ind=="West Bengal", "East",
                                                                      # Western
                                                                      ifelse(ex_state_ind=="Daman and Diu" | ex_state_ind=="Goa" | ex_state_ind=="Maharashtra" | ex_state_ind=="Gujarat", "West",
                                                                             # Southern
                                                                             ifelse(ex_state_ind=="Andaman and Nicobar" | ex_state_ind=="Andhra Pradesh" | ex_state_ind=="Karnataka" | ex_state_ind=="Kerala" | ex_state_ind=="Puducherry" | ex_state_ind=="Tamil Nadu" | ex_state_ind=="Telangana", "South", NA))))))))





####### Adding state labels
prevtotcontrolled <- mutate(prevtotcontrolled, state_lab = fct_recode(ex_state_ind, 
                                                                "HP" = "Himachal Pradesh",
                                                                "PB" = "Punjab",
                                                                "CH" = "Chandigarh",
                                                                "HR" = "Haryana",
                                                                "DL" = "Delhi",
                                                                "SK" = "Sikkim",
                                                                "DD" = "Daman and Diu",
                                                                "AR" = "Arunachal Pradesh",
                                                                "NL" = "Nagaland",
                                                                "MN" = "Manipur",
                                                                "MZ" = "Mizoram",
                                                                "TR" = "Tripura",
                                                                "ML" = "Meghalaya",
                                                                "WB" = "West Bengal",
                                                                "MH" = "Maharashtra",
                                                                "AP" = "Andhra Pradesh",
                                                                "KA" = "Karnataka",
                                                                "GA" = "Goa",
                                                                "KL" = "Kerala",
                                                                "PY" = "Puducherry",
                                                                "TN" = "Tamil Nadu",
                                                                "AN" = "Andaman and Nicobar Islands",
                                                                "TS" = "Telangana",
                                                                "UK" = "Uttarakhand",
                                                                "RJ" = "Rajasthan",
                                                                "UP" = "Uttar Pradesh",
                                                                "BR" = "Bihar",
                                                                "AS" = "Assam",
                                                                "JK" = "Jammu and Kashmir",
                                                                "GJ" = "Gujarat",
                                                                "JH" = "Jharkhand",
                                                                "OD" = "Odisha",
                                                                "CT" = "Chhattisgarh", 
                                                                "MP" = "Madhya Pradesh"))



prevtotcontrolled <- mutate(prevtotcontrolled, 
                         urban_dbl = as.numeric(urban))

#install.packages("spatstat")


prevtotcontrolled <- mutate(prevtotcontrolled,
                         state_PCI = ifelse(ex_state_ind== "Andaman and Nicobar Islands",161595.035*(1/16.734), 
                                            ifelse(ex_state_ind== "Andhra Pradesh",93989.48897*(1/16.734),
                                                   ifelse(ex_state_ind== "Arunachal Pradesh",97887.8059*(1/16.734),
                                                          ifelse(ex_state_ind== "Assam",51099.8419*(1/16.734),
                                                                 ifelse(ex_state_ind== "Bihar",33012.94996*(1/16.734),
                                                                        ifelse(ex_state_ind== "Chandigarh",275484.3905*(1/16.734),
                                                                               ifelse(ex_state_ind== "Chhattisgarh",72687.6339*(1/16.734),
                                                                                      ifelse(ex_state_ind== "Delhi",232979.7323*(1/16.734),
                                                                                             ifelse(ex_state_ind== "Goa",335245.0559*(1/16.734),
                                                                                                    ifelse(ex_state_ind== "Gujarat",126678.0115*(1/16.734),
                                                                                                           ifelse(ex_state_ind== "Haryana",153410.0874*(1/16.734),
                                                                                                                  ifelse(ex_state_ind== "Himachal Pradesh",120305.5909*(1/16.734),
                                                                                                                         ifelse(ex_state_ind== "Jammu and Kashmir",69825.28608*(1/16.734),
                                                                                                                                ifelse(ex_state_ind== "Jharkhand",52374.28707*(1/16.734),
                                                                                                                                       ifelse(ex_state_ind== "Karnataka",100598.087*(1/16.734),
                                                                                                                                              ifelse(ex_state_ind== "Kerala",118625.7787*(1/16.734),
                                                                                                                                                     ifelse(ex_state_ind== "Madhya Pradesh",59858.0615*(1/16.734),
                                                                                                                                                            ifelse(ex_state_ind== "Maharashtra",134384.0679*(1/16.734),
                                                                                                                                                                   ifelse(ex_state_ind== "Manipur",50157.67944*(1/16.734),
                                                                                                                                                                          ifelse(ex_state_ind== "Meghalaya",73888.84451*(1/16.734),
                                                                                                                                                                                 ifelse(ex_state_ind== "Mizoram",93847.46347*(1/16.734),
                                                                                                                                                                                        ifelse(ex_state_ind== "Nagaland",89709.28511*(1/16.734),
                                                                                                                                                                                               ifelse(ex_state_ind== "Odisha",65035.16039*(1/16.734),
                                                                                                                                                                                                      ifelse(ex_state_ind== "Puducherry",168892.5785*(1/16.734),
                                                                                                                                                                                                             ifelse(ex_state_ind== "Punjab",114462.0737*(1/16.734),
                                                                                                                                                                                                                    ifelse(ex_state_ind== "Rajasthan",75510.83915*(1/16.734),
                                                                                                                                                                                                                           ifelse(ex_state_ind== "Sikkim",202709.8957*(1/16.734),
                                                                                                                                                                                                                                  ifelse(ex_state_ind== "Tamil Nadu",118402.3791*(1/16.734),
                                                                                                                                                                                                                                         ifelse(ex_state_ind== "Tripura",72973.88591*(1/16.734),
                                                                                                                                                                                                                                                ifelse(ex_state_ind== "Uttar Pradesh",43177.81353*(1/16.734),
                                                                                                                                                                                                                                                       ifelse(ex_state_ind== "Uttarakhand",121845.5702*(1/16.734),
                                                                                                                                                                                                                                                              ifelse(ex_state_ind== "West Bengal",77409.18859*(1/16.734),
                                                                                                                                                                                                                                                                     ifelse(ex_state_ind== "Telangana",111311.9409*(1/16.734),NA))))))))))))))))))))))))))))))))))







prevtotcontrolled <- dplyr::filter(prevtotcontrolled, ex_state_ind != "Puducherry") 
prevtotcontrolled <- dplyr::filter(prevtotcontrolled, ex_state_ind != "Daman and Diu")
prevtotcontrolled <- dplyr::filter(prevtotcontrolled, ex_state_ind != "Delhi")
prevtotcontrolled <- dplyr::filter(prevtotcontrolled, ex_state_ind != "Dadra and Nagar Haveli")
prevtotcontrolled <- dplyr::filter(prevtotcontrolled, ex_state_ind != "Andaman and Nicobar Islands")
prevtotcontrolled <- dplyr::filter(prevtotcontrolled, ex_state_ind != "Lakshadweep")
prevtotcontrolled <- dplyr::filter(prevtotcontrolled, ex_state_ind != "Chandigarh")

write.csv(prevtotcontrolled, "combined cascade controlled gdp per capita.csv")

#####controlled 

controlledbygdp <- prevtotcontrolled %>% 
  ggplot() +
  geom_smooth(aes(y=htn_controlled, x=state_PCI), color="grey50", fill="grey80", alpha=0.7, 
              se=F, method="lm", size=0.7, fullrange=T) +
  geom_point(aes(y= htn_controlled, x=state_PCI, color=as.factor(zone)), size=3) +
  geom_text_repel(mapping=aes(y=htn_controlled, x=state_PCI, 
                              label = state_lab, fontface=2), 
                  size = 5, segment.color = "black", segment.size = 0.3, family="Times") +
  geom_errorbar(aes(ymin=htn_low, ymax=htn_upp, x=state_PCI, color=as.factor(zone)), 
                width=0.05, alpha=0.5, show.legend = F) +
  theme_classic() + 
  labs(x = "GDP per capita (int. $)",
       y = "Percent",
       fill="") + 
  theme(axis.text.y=element_text(size=20),
        axis.text.x=element_text(size=20),
        axis.title=element_text(size=22),
        legend.text=element_text(size=15, family="Times"),
        legend.title = element_blank(),
        #legend.position="bottom",
        axis.title.x = element_text(margin = margin(t = 20)),
        axis.title.y = element_text(margin = margin(r = 20)),
        strip.text.x = element_text(size=22),
        strip.background = element_blank(),
        panel.spacing = unit(2, "lines"),
        title = element_text(size=24, face="bold", family="Times")) + 
  scale_color_brewer(palette="Set1") +
  scale_y_continuous(breaks = seq(from=0, to=100, by=20), limits=c(0, 140)) +
  scale_x_continuous(breaks = seq(from=0, to=25000, by=5000), limits=c(0, 25000)) +
  coord_fixed(25000/100, expand=F)
controlledbygdp 




```








```{r making fast tables}

library(stringr)

  cascades.melt <- mutate(cascades.melt,
                          cilow = str_sub(lowci, 1, 4),
         cihigh = str_sub(uppci, 1, 4),
         citemp = str_c(cilow, cihigh, sep="-"),
         bracketstart = "(", 
         bracketend = ")",
         ci = str_c(bracketstart, citemp, bracketend, sep=""),
         rrshort = str_sub(value, 1, 4),
         rr = str_c(rrshort, ci, sep=" ")) 



```








```{r aware-sum multi figure}

#####dummy variables for each number of morbidites

dhs_nomiss <- mutate(dhs_nomiss, 
                    zero_morb = ifelse(sum_multi==0,1,0),
                    one_morb = ifelse(sum_multi==1,1,0),
                     two_morb = ifelse(sum_multi==2,1,0),
                     three_morb = ifelse(sum_multi==3,1,0),
                     four_morb = ifelse(sum_multi==4,1,0),
                     five_morb = ifelse(sum_multi==5,1,0),
                     six_morb = ifelse(sum_multi==6,1,0),
                     seven_morb = ifelse(sum_multi==7,1,0),
                     eight_morb = ifelse(sum_multi==8,1,0))
               
               



```


```{r heatmap aware multimorb}


dhs_nomiss_diabetic <- filter(dhs_nomiss, (ex_diab_broad_ind)==1)

multi_heatmap <- dhs_nomiss_diabetic %>%
  filter(is.na(wealth_quintile_rurb)==FALSE & is.na(age)==FALSE) %>% 
  group_by( sum_multidiab, age_grp, urban_lab) %>% 
  mutate(multi_morbid_indiv = 100*weighted.mean(diabetic_aware_dbl, p_wt_new, na.rm=TRUE)) %>%
  filter(row_number()==1) %>%   
  dplyr::select( sum_multidiab, age_grp, urban_lab, multi_morbid_indiv, wealth_quintile_rurb_lab, urban)

# Now create the actual heatmap: 
multi_heat <- ggplot(data=multi_heatmap, aes(x=sum_multidiab, y=age_grp)) +
  geom_tile(aes(fill=multi_morbid_indiv)) + 
  geom_text(aes(label=sprintf("%1.1f", multi_morbid_indiv)), size=5) +
  scale_fill_distiller(palette = "RdYlGn", direction = -1) +
  facet_grid(. ~urban_lab) +
  theme_classic() +
  labs(x = "Household Wealth Quintile",
       y = "Age Group, y",
       fill="") +
  theme(axis.text.x =element_text(size=15, face="bold", family="Times", angle=45, hjust=1),
        axis.text.y =element_text(size=15, face="bold", family="Times"),
        axis.title=element_text(size=22, face="italic", family="Times"),
        legend.title=element_blank(),
        legend.text=element_text(size=18, family="Times"),
        strip.text=element_text(size=18, family="Times", face="bold"), 
        panel.spacing = unit(2, "lines"),
        axis.title.x = element_text(margin = margin(t = 20), family="Times"),
        axis.title.y = element_text(margin = margin(r = 20), family="Times"),
        plot.title = element_text(size = 25, hjust = 0.5, family="Times", face = "bold")) +
  coord_fixed(5/4)
multi_heat
```




```{r diabetes cascade with obese}

##########################Diabetes cascade with obese comorbidity
  
 #filter diabetics only
dhs_nomiss_diabetic_obese_only <- filter(dhs_nomiss, (ex_diab_broad_ind)==1 & obese==1) 


##aware diabetic


##aware diabetic as subset of diabetics



dhs_nomiss_diabetic_obese_only <- mutate(dhs_nomiss_diabetic_obese_only,
                     diabetic_aware = ifelse(hbg12==1, 1, 0))

dhs_nomiss_diabetic_obese_only$diabetic_aware <- as.factor(dhs_nomiss_diabetic_obese_only$diabetic_aware)

dhs_nomiss_diabetic_obese_only <- mutate(dhs_nomiss_diabetic_obese_only,
                     
                     diabetic_aware_dbl = as.numeric(diabetic_aware)-1)

summary(dhs_nomiss_diabetic_obese_only$diabetic_aware)

##treated diabetic as subset diabetics


dhs_nomiss_diabetic_obese_only <- mutate(dhs_nomiss_diabetic_obese_only,
                                   diabetic_treated = ifelse(ex_dia_med_ind==1, 1, 0))
dhs_nomiss_diabetic_obese_only[which(is.na(dhs_nomiss_diabetic_obese_only$diabetic_treated)==T), "diabetic_treated"]<-0

dhs_nomiss_diabetic_obese_only$diabetic_treated <- as.factor(dhs_nomiss_diabetic_obese_only$diabetic_treated)

dhs_nomiss_diabetic_obese_only <- mutate(dhs_nomiss_diabetic_obese_only,
                                   
                                   diabetic_treated_dbl = as.numeric(diabetic_treated)-1)

summary(dhs_nomiss_diabetic_obese_only$diabetic_treated)

##controlled diabetic as subset of  diabetics

dhs_nomiss_diabetic_obese_only <- mutate(dhs_nomiss_diabetic_obese_only,
                                             diabetic_controlled = ifelse((ex_dia_med_ind==1 & ex_diab_narrow_ind==0), 1, 0))

dhs_nomiss_diabetic_obese_only$diabetic_controlled <- as.factor(dhs_nomiss_diabetic_obese_only$diabetic_controlled)

dhs_nomiss_diabetic_obese_only <- mutate(dhs_nomiss_diabetic_obese_only,
                                             
                                             diabetic_controlled_dbl = as.numeric(diabetic_controlled)-1)

summary(dhs_nomiss_diabetic_obese_only$diabetic_controlled)

## Test if there are paradoxically controlled but aware patients
dhs_nomiss_diabetic_obese_only <- mutate(dhs_nomiss_diabetic_obese_only,
                                   controlled_and_unaware = ifelse(diabetic_aware==0 & diabetic_controlled==1, 1, 0))
dhs_nomiss_diabetic_obese_only$controlled_and_unaware <- as.factor(dhs_nomiss_diabetic_obese_only$controlled_and_unaware)
summary(dhs_nomiss_diabetic_obese_only$controlled_and_unaware)


###create PSU ID in dhs_nomiss_diabetic_obese_only :
dhs_nomiss_diabetic_obese_only <- dhs_nomiss_diabetic_obese_only %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_diabetic_obese_only$psuid <- as.factor(dhs_nomiss_diabetic_obese_only$psuid)

###make noNAinpsu

dhs_nomiss_diabetic_obese_only_noNAinpsu <- filter(dhs_nomiss_diabetic_obese_only, is.na(psu)==F)

summary(dhs_nomiss_diabetic_obese_only_noNAinpsu$psu)

sum(is.na(dhs_nomiss_diabetic_obese_only_noNAinpsu$psu)==T)




##### Crude controlled diabetes prev 

svy_controlled <- dhs_nomiss_diabetic_obese_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_controlled_dbl))

prevtot <- svy_controlled %>% 
  summarize(diab_controlled_prop = survey_mean(diabetic_controlled_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(diab_controlled = 100*diab_controlled_prop,
         diab_low = 100*diab_controlled_prop_low,
         diab_upp = 100*diab_controlled_prop_upp)



write_csv(prevtot, "prevcontrolled diab with obese _2018-04-21.csv")

#### Crude treated diabetes prev 


svy_treated <- dhs_nomiss_diabetic_obese_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_treated_dbl))

prevtot <- svy_treated %>%
  summarize(diab_treated_prop = survey_mean(diabetic_treated_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(diab_treated = 100*diab_treated_prop,
         diab_low = 100*diab_treated_prop_low,
         diab_upp = 100*diab_treated_prop_upp)


write_csv(prevtot, "prevtreated diab with obese _2018-04-21.csv")

#### Crude aware diabetes prev  


svy_aware <- dhs_nomiss_diabetic_obese_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_aware_dbl))

prevtot <- svy_aware %>%
  summarize(diab_aware_prop = survey_mean(diabetic_aware_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(diab_aware = 100*diab_aware_prop,
         diab_low = 100*diab_aware_prop_low,
         diab_upp = 100*diab_aware_prop_upp)


write_csv(prevtot, "prevaware diab with obese _2018-04-21.csv")


###############Comparison diabetic no-obese



#filter diabetics no htn
dhs_nomiss_diabetic_noobese_only <- filter(dhs_nomiss, (ex_diab_broad_ind)==1 & obese==0) 


##aware diabetic



## Test if there are paradoxically treated but aware patients
dhs_nomiss_diabetic_noobese_only <- mutate(dhs_nomiss_diabetic_noobese_only,
                     treated_and_unaware = ifelse(hbg12==0 & ex_dia_med_ind==1, 1, 0))
dhs_nomiss_diabetic_noobese_only$treated_and_unaware <- as.factor(dhs_nomiss_diabetic_noobese_only$treated_and_unaware)
summary(dhs_nomiss_diabetic_noobese_only$treated_and_unaware)


## Test if there are paradoxically controlled but aware patients
dhs_nomiss_diabetic_noobese_only <- mutate(dhs_nomiss_diabetic_noobese_only,
                                   treated_and_unaware = ifelse(hbg12==0 & ex_dia_med_ind==1, 1, 0))
dhs_nomiss_diabetic_noobese_only$treated_and_unaware <- as.factor(dhs_nomiss_diabetic_noobese_only$treated_and_unaware)
summary(dhs_nomiss_diabetic_noobese_only$treated_and_unaware)




##aware diabetic as subset of diabetics



dhs_nomiss_diabetic_noobese_only <- mutate(dhs_nomiss_diabetic_noobese_only,
                     diabetic_aware = ifelse(hbg12==1, 1, 0))

dhs_nomiss_diabetic_noobese_only$diabetic_aware <- as.factor(dhs_nomiss_diabetic_noobese_only$diabetic_aware)

dhs_nomiss_diabetic_noobese_only <- mutate(dhs_nomiss_diabetic_noobese_only,
                     
                     diabetic_aware_dbl = as.numeric(diabetic_aware)-1)

summary(dhs_nomiss_diabetic_noobese_only$diabetic_aware)

##treated diabetic as subset diabetics


dhs_nomiss_diabetic_noobese_only <- mutate(dhs_nomiss_diabetic_noobese_only,
                                   diabetic_treated = ifelse(ex_dia_med_ind==1, 1, 0))
dhs_nomiss_diabetic_noobese_only[which(is.na(dhs_nomiss_diabetic_noobese_only$diabetic_treated)==T), "diabetic_treated"]<-0

dhs_nomiss_diabetic_noobese_only$diabetic_treated <- as.factor(dhs_nomiss_diabetic_noobese_only$diabetic_treated)

dhs_nomiss_diabetic_noobese_only <- mutate(dhs_nomiss_diabetic_noobese_only,
                                   
                                   diabetic_treated_dbl = as.numeric(diabetic_treated)-1)

summary(dhs_nomiss_diabetic_noobese_only$diabetic_treated)

##controlled diabetic as subset of  diabetics

dhs_nomiss_diabetic_noobese_only <- mutate(dhs_nomiss_diabetic_noobese_only,
                                             diabetic_controlled = ifelse((ex_dia_med_ind==1 & ex_diab_narrow_ind==0), 1, 0))

dhs_nomiss_diabetic_noobese_only$diabetic_controlled <- as.factor(dhs_nomiss_diabetic_noobese_only$diabetic_controlled)

dhs_nomiss_diabetic_noobese_only <- mutate(dhs_nomiss_diabetic_noobese_only,
                                             
                                             diabetic_controlled_dbl = as.numeric(diabetic_controlled)-1)

summary(dhs_nomiss_diabetic_noobese_only$diabetic_controlled)

## Test if there are paradoxically controlled but aware patients
dhs_nomiss_diabetic_noobese_only <- mutate(dhs_nomiss_diabetic_noobese_only,
                                   controlled_and_unaware = ifelse(diabetic_aware==0 & diabetic_controlled==1, 1, 0))
dhs_nomiss_diabetic_noobese_only$controlled_and_unaware <- as.factor(dhs_nomiss_diabetic_noobese_only$controlled_and_unaware)
summary(dhs_nomiss_diabetic_noobese_only$controlled_and_unaware)


###Nainpsu for dhs_nomiss_diabetic_noobese_only

dhs_nomiss_diabetic_noobese_only <- mutate(dhs_nomiss_diabetic_noobese_only, 
                     psu = ifelse(psu==1, NA, psu))

summary(dhs_nomiss_diabetic_noobese_only$psu)

dhs_nomiss_diabetic_noobese_only_noNAinpsu <- filter(dhs_nomiss_diabetic_noobese_only, is.na(psu)==F)

summary(dhs_nomiss_diabetic_noobese_only_noNAinpsu$psu)


##### Crude controlled diabetes prev 

svy_controlled <- dhs_nomiss_diabetic_noobese_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_controlled_dbl))

prevtot <- svy_controlled %>% 
  summarize(diab_controlled_prop = survey_mean(diabetic_controlled_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(diab_controlled = 100*diab_controlled_prop,
         diab_low = 100*diab_controlled_prop_low,
         diab_upp = 100*diab_controlled_prop_upp)



write_csv(prevtot, "prevcontrolled diab noobese _2018-04-21.csv")

#### Crude treated diabetes prev 


svy_treated <- dhs_nomiss_diabetic_noobese_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_treated_dbl))

prevtot <- svy_treated %>%
  summarize(diab_treated_prop = survey_mean(diabetic_treated_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(diab_treated = 100*diab_treated_prop,
         diab_low = 100*diab_treated_prop_low,
         diab_upp = 100*diab_treated_prop_upp)


write_csv(prevtot, "prevtreated diab noobese _2018-04-21.csv")

#### Crude aware diabetes prev  


svy_aware <- dhs_nomiss_diabetic_noobese_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(diabetic_aware_dbl))

prevtot <- svy_aware %>%
  summarize(diab_aware_prop = survey_mean(diabetic_aware_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(diab_aware = 100*diab_aware_prop,
         diab_low = 100*diab_aware_prop_low,
         diab_upp = 100*diab_aware_prop_upp)


write_csv(prevtot, "prevaware diab noobese _2018-04-21.csv")

```


```{r hypertension cascades with obese}



#######################hypertension cascade with obese comorbidity
  
 #filter htns only
dhs_nomiss_htn_obese_only <- filter(dhs_nomiss, (ex_htn_broad_ind)==1 & obese==1) 




##screened htn

dhs_nomiss_htn_obese_only <- mutate(dhs_nomiss_htn_obese_only,
                     htn_screened = bp_ms)

dhs_nomiss_htn_obese_only$htn_screened <- as.factor(dhs_nomiss_htn_obese_only$htn_screened)

dhs_nomiss_htn_obese_only <- mutate(dhs_nomiss_htn_obese_only,
                              
                              htn_screened_dbl = as.numeric(htn_screened)-1)

summary(dhs_nomiss_htn_obese_only$htn_screened)


##aware htn



## Test if there are paradoxically treated but unaware patients
dhs_nomiss_htn_obese_only <- mutate(dhs_nomiss_htn_obese_only,
                     treated_and_unaware = ifelse( hypt==0 &hypt_med==1, 1, 0))
dhs_nomiss_htn_obese_only$treated_and_unaware <- as.factor(dhs_nomiss_htn_obese_only$treated_and_unaware)
summary(dhs_nomiss_htn_obese_only$treated_and_unaware)

dhs_nomiss_htn_obese_only <- mutate(dhs_nomiss_htn_obese_only,
                             treated_and_unaware = ifelse( htn_aware==0 &htn_treated==1, 1, 0))
dhs_nomiss_htn_obese_only$treated_and_unaware <- as.factor(dhs_nomiss_htn_obese_only$treated_and_unaware)
summary(dhs_nomiss_htn_obese_only$treated_and_unaware)

##aware htn as subset of htn



dhs_nomiss_htn_obese_only <- mutate(dhs_nomiss_htn_obese_only,
                     htn_aware = ifelse(hypt==1 | hypt_med==1, 1, 0))


dhs_nomiss_htn_obese_only$htn_aware <- as.factor(dhs_nomiss_htn_obese_only$htn_aware)



dhs_nomiss_htn_obese_only <- mutate(dhs_nomiss_htn_obese_only,
                     
                     htn_aware_dbl = as.numeric(htn_aware)-1)

summary(dhs_nomiss_htn_obese_only$htn_aware)

##treated htn as subset of htns


dhs_nomiss_htn_obese_only <- mutate(dhs_nomiss_htn_obese_only,
                                   htn_treated = ifelse(hypt_med==1, 1, 0))
#dhs_nomiss_htn_obese_only[which(is.na(dhs_nomiss_htn_obese_only$htn_treated)==T), "htn_treated"]<-0

dhs_nomiss_htn_obese_only$htn_treated <- as.factor(dhs_nomiss_htn_obese_only$htn_treated)

dhs_nomiss_htn_obese_only <- mutate(dhs_nomiss_htn_obese_only,
                                   
                                   htn_treated_dbl = as.numeric(htn_treated)-1)

summary(dhs_nomiss_htn_obese_only$htn_treated)

##controlled htn as subset of  htns

dhs_nomiss_htn_obese_only <- mutate(dhs_nomiss_htn_obese_only,
                                             htn_controlled = ifelse((hypt_med==1 & ex_htn_narrow_ind==0), 1, 0))

dhs_nomiss_htn_obese_only$htn_controlled <- as.factor(dhs_nomiss_htn_obese_only$htn_controlled)

dhs_nomiss_htn_obese_only <- mutate(dhs_nomiss_htn_obese_only,
                                             
                                             htn_controlled_dbl = as.numeric(htn_controlled)-1)

summary(dhs_nomiss_htn_obese_only$htn_controlled)


###paradoxically aware but unscreened

dhs_nomiss_htn_obese_only <- mutate(dhs_nomiss_htn_obese_only,
                              aware_and_unscreened = ifelse( htn_aware==1 & htn_screened==0, 1, 0))
dhs_nomiss_htn_obese_only$aware_and_unscreened <- as.factor(dhs_nomiss_htn_obese_only$aware_and_unscreened)
summary(dhs_nomiss_htn_obese_only$aware_and_unscreened)


###paradoxically treated and unscreened

dhs_nomiss_htn_obese_only <- mutate(dhs_nomiss_htn_obese_only,
                              treated_and_unscreened = ifelse( htn_treated==1 & htn_screened==0, 1, 0))
dhs_nomiss_htn_obese_only$treated_and_unscreened <- as.factor(dhs_nomiss_htn_obese_only$treated_and_unscreened)
summary(dhs_nomiss_htn_obese_only$treated_and_unscreened)

## Test if there are paradoxically treated but unaware patients
dhs_nomiss_htn_obese_only <- mutate(dhs_nomiss_htn_obese_only,
                     treated_and_unaware = ifelse( htn_aware==0 & htn_treated==1, 1, 0))
dhs_nomiss_htn_obese_only$treated_and_unaware <- as.factor(dhs_nomiss_htn_obese_only$treated_and_unaware)
summary(dhs_nomiss_htn_obese_only$treated_and_unaware)

###create PSU ID in dhs_nomiss_htn_obese_only :
dhs_nomiss_htn_obese_only <- dhs_nomiss_htn_obese_only %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_htn_obese_only$psuid <- as.factor(dhs_nomiss_htn_obese_only$psuid)

###make noNAinpsu

dhs_nomiss_htn_obese_only_noNAinpsu <- filter(dhs_nomiss_htn_obese_only, is.na(psu)==F)

summary(dhs_nomiss_htn_obese_only_noNAinpsu$psu)

sum(is.na(dhs_nomiss_htn_obese_only_noNAinpsu$psu)==T)



##### Crude controlled htn prev 

svy_controlled <- dhs_nomiss_htn_obese_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_controlled_dbl))

prevtot <- svy_controlled %>% 
  summarize(htn_controlled_prop = survey_mean(htn_controlled_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(htn_controlled = 100*htn_controlled_prop,
         htn_low = 100*htn_controlled_prop_low,
         htn_upp = 100*htn_controlled_prop_upp)



write_csv(prevtot, "htnprevcontrolled com diab _2018-03_22.csv")

#### Crude treated htn prev 


svy_treated <- dhs_nomiss_htn_obese_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_treated_dbl))

prevtot <- svy_treated %>%
  summarize(htn_treated_prop = survey_mean(htn_treated_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(htn_treated = 100*htn_treated_prop,
         htn_low = 100*htn_treated_prop_low,
         htn_upp = 100*htn_treated_prop_upp)


write_csv(prevtot, "htnprevtreated com diab _2018-03_22.csv")

#### Crude aware htn prev  


svy_aware <- dhs_nomiss_htn_obese_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_aware_dbl))

prevtot <- svy_aware %>%
  summarize(htn_aware_prop = survey_mean(htn_aware_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(htn_aware = 100*htn_aware_prop,
         htn_low = 100*htn_aware_prop_low,
         htn_upp = 100*htn_aware_prop_upp)


write_csv(prevtot, "htnprevaware com diab_2018-03_22.csv")

#### Crude screened htn prev  


svy_screened <- dhs_nomiss_htn_obese_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_screened_dbl))

prevtot <- svy_screened %>%
  summarize(htn_screened_prop = survey_mean(htn_screened_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(htn_screened = 100*htn_screened_prop,
         htn_low = 100*htn_screened_prop_low,
         htn_upp = 100*htn_screened_prop_upp)


write_csv(prevtot, "htnprevscreened com diab_2018-03_22.csv")


########Comparison htn no diab

#filter htn no diab
dhs_nomiss_noobese_htn_only <- filter(dhs_nomiss, (ex_htn_broad_ind)==1 & obese==0) 



##screened htn

dhs_nomiss_noobese_htn_only <- mutate(dhs_nomiss_noobese_htn_only,
                     htn_screened = bp_ms)

dhs_nomiss_noobese_htn_only$htn_screened <- as.factor(dhs_nomiss_noobese_htn_only$htn_screened)

dhs_nomiss_noobese_htn_only <- mutate(dhs_nomiss_noobese_htn_only,
                              
                              htn_screened_dbl = as.numeric(htn_screened)-1)

summary(dhs_nomiss_noobese_htn_only$htn_screened)


##aware htn



## Test if there are paradoxically treated but unaware patients
dhs_nomiss_noobese_htn_only <- mutate(dhs_nomiss_noobese_htn_only,
                     treated_and_unaware = ifelse( hypt==0 &hypt_med==1, 1, 0))
dhs_nomiss_noobese_htn_only$treated_and_unaware <- as.factor(dhs_nomiss_noobese_htn_only$treated_and_unaware)
summary(dhs_nomiss_noobese_htn_only$treated_and_unaware)

dhs_nomiss_noobese_htn_only <- mutate(dhs_nomiss_noobese_htn_only,
                             treated_and_unaware = ifelse( htn_aware==0 &htn_treated==1, 1, 0))
dhs_nomiss_noobese_htn_only$treated_and_unaware <- as.factor(dhs_nomiss_noobese_htn_only$treated_and_unaware)
summary(dhs_nomiss_noobese_htn_only$treated_and_unaware)

##aware htn as subset of htn



dhs_nomiss_noobese_htn_only <- mutate(dhs_nomiss_noobese_htn_only,
                     htn_aware = ifelse(hypt==1 | hypt_med==1, 1, 0))


dhs_nomiss_noobese_htn_only$htn_aware <- as.factor(dhs_nomiss_noobese_htn_only$htn_aware)



dhs_nomiss_noobese_htn_only <- mutate(dhs_nomiss_noobese_htn_only,
                     
                     htn_aware_dbl = as.numeric(htn_aware)-1)

summary(dhs_nomiss_noobese_htn_only$htn_aware)

##treated htn as subset of htns


dhs_nomiss_noobese_htn_only <- mutate(dhs_nomiss_noobese_htn_only,
                                   htn_treated = ifelse(hypt_med==1, 1, 0))
#dhs_nomiss_noobese_htn_only[which(is.na(dhs_nomiss_noobese_htn_only$htn_treated)==T), "htn_treated"]<-0

dhs_nomiss_noobese_htn_only$htn_treated <- as.factor(dhs_nomiss_noobese_htn_only$htn_treated)

dhs_nomiss_noobese_htn_only <- mutate(dhs_nomiss_noobese_htn_only,
                                   
                                   htn_treated_dbl = as.numeric(htn_treated)-1)

summary(dhs_nomiss_noobese_htn_only$htn_treated)

##controlled htn as subset of  htns

dhs_nomiss_noobese_htn_only <- mutate(dhs_nomiss_noobese_htn_only,
                                             htn_controlled = ifelse((hypt_med==1 & ex_htn_narrow_ind==0), 1, 0))

dhs_nomiss_noobese_htn_only$htn_controlled <- as.factor(dhs_nomiss_noobese_htn_only$htn_controlled)

dhs_nomiss_noobese_htn_only <- mutate(dhs_nomiss_noobese_htn_only,
                                             
                                             htn_controlled_dbl = as.numeric(htn_controlled)-1)

summary(dhs_nomiss_noobese_htn_only$htn_controlled)


###paradoxically aware but unscreened

dhs_nomiss_noobese_htn_only <- mutate(dhs_nomiss_noobese_htn_only,
                              aware_and_unscreened = ifelse( htn_aware==1 & htn_screened==0, 1, 0))
dhs_nomiss_noobese_htn_only$aware_and_unscreened <- as.factor(dhs_nomiss_noobese_htn_only$aware_and_unscreened)
summary(dhs_nomiss_noobese_htn_only$aware_and_unscreened)


###paradoxically treated and unscreened

dhs_nomiss_noobese_htn_only <- mutate(dhs_nomiss_noobese_htn_only,
                              treated_and_unscreened = ifelse( htn_treated==1 & htn_screened==0, 1, 0))
dhs_nomiss_noobese_htn_only$treated_and_unscreened <- as.factor(dhs_nomiss_noobese_htn_only$treated_and_unscreened)
summary(dhs_nomiss_noobese_htn_only$treated_and_unscreened)

## Test if there are paradoxically treated but unaware patients
dhs_nomiss_noobese_htn_only <- mutate(dhs_nomiss_noobese_htn_only,
                     treated_and_unaware = ifelse( htn_aware==0 & htn_treated==1, 1, 0))
dhs_nomiss_noobese_htn_only$treated_and_unaware <- as.factor(dhs_nomiss_noobese_htn_only$treated_and_unaware)
summary(dhs_nomiss_noobese_htn_only$treated_and_unaware)

###create PSU ID in dhs_nomiss_noobese_htn_only :
dhs_nomiss_noobese_htn_only <- dhs_nomiss_noobese_htn_only %>% 
  mutate(psu_str = as.character(psu), 
         psuid = str_c(state_dist_str, psu_str, sep = "_"))
dhs_nomiss_noobese_htn_only$psuid <- as.factor(dhs_nomiss_noobese_htn_only$psuid)

###make noNAinpsu

dhs_nomiss_noobese_htn_only_noNAinpsu <- filter(dhs_nomiss_noobese_htn_only, is.na(psu)==F)

summary(dhs_nomiss_noobese_htn_only_noNAinpsu$psu)

sum(is.na(dhs_nomiss_noobese_htn_only_noNAinpsu$psu)==T)



##### Crude controlled htn prev 

svy_controlled <- dhs_nomiss_noobese_htn_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_controlled_dbl))

prevtot <- svy_controlled %>% 
  summarize(htn_controlled_prop = survey_mean(htn_controlled_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(htn_controlled = 100*htn_controlled_prop,
         htn_low = 100*htn_controlled_prop_low,
         htn_upp = 100*htn_controlled_prop_upp)



write_csv(prevtot, "htnprevcontrolled no diab _2018-03_22.csv")

#### Crude treated htn prev 


svy_treated <- dhs_nomiss_noobese_htn_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_treated_dbl))

prevtot <- svy_treated %>%
  summarize(htn_treated_prop = survey_mean(htn_treated_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(htn_treated = 100*htn_treated_prop,
         htn_low = 100*htn_treated_prop_low,
         htn_upp = 100*htn_treated_prop_upp)


write_csv(prevtot, "htnprevtreated no diab _2018-03_22.csv")

#### Crude aware htn prev  


svy_aware <- dhs_nomiss_noobese_htn_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_aware_dbl))

prevtot <- svy_aware %>%
  summarize(htn_aware_prop = survey_mean(htn_aware_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(htn_aware = 100*htn_aware_prop,
         htn_low = 100*htn_aware_prop_low,
         htn_upp = 100*htn_aware_prop_upp)


write_csv(prevtot, "htnprevaware no diab_2018-03_22.csv")

#### Crude screened htn prev  


svy_screened <- dhs_nomiss_noobese_htn_only_noNAinpsu %>% 
  as_survey_design(stratum = stratum,
                   ids = c(psuid, hh_id),
                   weights = p_wt_new,
                   variables = c(htn_screened_dbl))

prevtot <- svy_screened %>%
  summarize(htn_screened_prop = survey_mean(htn_screened_dbl, proportion=TRUE, vartype = "ci")) %>% 
  mutate(htn_screened = 100*htn_screened_prop,
         htn_low = 100*htn_screened_prop_low,
         htn_upp = 100*htn_screened_prop_upp)


write_csv(prevtot, "htnprevscreened no diab_2018-03_22.csv")


```



```{testcode}

 glm asthma_dbl sex educat csmoke d_id_dbl, fam(poisson) link(log) nolog vce(clus
> ter psu) eform

dhs$d_id_dbl <- as.numeric(dhs$d_id)
dhs$asthma_dbl <- as.numeric(dhs$asthma)


 diabetes_aware <- glm.cluster(formula = sex  ~ marital + educat + csmoke + d_id ,  cluster="psu", data=dhs, family=poisson(link="log"))

 aware <- exp(cbind(RR = coef(diabetes_aware), confint(diabetes_aware)))
write.csv(aware, "RR diabetes aware poisson.csv")

results_aware <-summary(diabetes_aware)
write.csv(results_aware, "p Value diabetes awarepoisson.csv")

